How Can I Get This PHP Registration Script To Redirect To Profile Page Based On User ID?

Viewed 65

I'm trying to redirect each user to a dedicated "userinfo.php" after successful registration, but the browser terminates the redirection because it goes into endless loops.

I need your help, please. Thanks.

The following is section of the "register.php" that has redirect:

<?php

    // include configuration file
    require ("includes/config.php");
        
            try
            {   
            $stmt = $pdo->prepare("SELECT username, email, phone FROM users WHERE username = ? OR email = ? OR phone = ?");
            $stmt->execute([$username, $email, $phone]);
            $user = $stmt->fetch(); # get users data
            
            if($user["username"]==$username)
            {
                 $usernameErr="Sorry username already exists"; //check condition username already exists
            }
            else if($user["email"]==$email)
            {
                $emailErr="Sorry email already exists"; //check condition email already exists 
            }
            else if($user["phone"]==$phone)
            {
                 $phoneErr="Sorry, the phone number already exists"; //check condition email already exists 
            }
            else //check no "$error" shows, then continue
            {
                $new_password = password_hash($password, PASSWORD_DEFAULT); //encrypt password using password_hash()

               // insert form input into database           
           $stmt=$pdo->prepare("INSERT INTO users (firstname, lastname, username, gender, password, email, birthday, phone, regdate, userimage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)");
          $stmt->execute([$firstname, $lastname, $username, $gender, $new_password, $email, $birthday, $phone, $imagename]);
                
               // find out user's ID
           $stmt = $pdo->prepare("SELECT LAST_INSERT_ID() AS user_id");
               $stmt->execute();
               $user_id = $pdo->lastInsertId();
            
               // redirect to list users page
               header("Location: userinfo.php?id=".$user_id);
            }
         }
                catch(PDOException $e)
            {
                echo $e->getMessage();
            }       
    
}
        // render the header template
        include("templates/header.php");
        
        // render add user template
        include("templates/register-form.php");
    
       // render the footer template
       include("templates/footer.php");
?>

My intention is that if a non-logged in user requests for any page, other than "admin/login.php", "admin/logout.php", and "admin/register.php", the person will be redirected to "admin/login.php".

Moreover, I want if a non-logged in user requests for any page in the front-end, except "login.php", "logout.php", and "register.php", the person will be redirected to "login.php".

I do have the following redirect code in the "config.php", which I suspect is causing the issue:

// require authentication for most pages
if (!preg_match("{(admin/login|logout|register)\.php$}", $_SERVER["PHP_SELF"]))
{
   if (empty($_SESSION["aid"]))
    {
       header("Location: login.php");
    }
}
if (!preg_match("{(?:index|login|logout|register)\.php$}", $_SERVER["PHP_SELF"]))
{
    if (empty($_SESSION["user_id"]))
    {
       header("Location: login.php");
    }
}
0 Answers
Related