I am setting both a COOKIE and SESSION for users when they login/signup. A SESSION for more usability and a COOKIE for persistence. When a new user signs up for my website, I'd like to redirect them to their profile. I need to create a SESSION and COOKIE variable as soon as they sign up to do this. Here is my solution:
$query2= "INSERT INTO users (fname, lname, email, password) VALUES
('$fname', '$lname', '$email', '$password1')";
mysqli_query($connect, $query2)
or die('error with query');
session_start();
$query3= "SELECT * FROM users where email= '".$email."' AND password=
'".$password1."'";
$result2= mysqli_query($connect, $query3);
$row= mysqli_fetch_array($result2);
$_SESSION['id'] = $row['user_id'];
setcookie('id', $row['user_id']);
echo $_SESSION['id'];
echo $_COOKIE['id'];
echo "You are now logged in.";
I first Insert the new row of data. Then I recall it to create the SESSION and COOKIE variable. But, here is my problem, whenever I echo the COOKIE variable it's value is always one less than what the actually value should be. The SESSION is okay though. What am I doing wrong? What could be the problem. I created another page.php to just echo out the SESSION and COOKIE variables and they are both correct there. Thanks.