2
<?php
//page 1
session_start();
// codes...
$_SESSION['user_name_loggedin'] = $user;
header("Location: profile.php");
// codes...
?>
<?php
//page2
session_start();
// codes...
if(isset($_SESSION['user_name_loggedin'])){
    echo $_SESSION['user_name_loggedin'];
}else{
    echo 'not set<br>';
}
// codes...
?>

I am trying to get a login working on my site using sessions. Above shows examples of the two pages I wish to transfer information between using a session. $user is taken from the login form on the login page. On the profile page, after logging in, it only shows 'not set'. Is there anything I am missing?

Thanks in advance

3
  • Does your browser allow cookies? Commented Mar 31, 2017 at 17:09
  • is $user serializeable? Commented Mar 31, 2017 at 17:09
  • Make sure $user has a value on the login page. If that value is not being retrieved from the form, the SESSION variable will not be set. Try printing out $user, and commenting out the redirect header. See if there is a value. Commented Mar 31, 2017 at 17:12

1 Answer 1

2

Tried it locally using the following code:

1 test.php

`<?php
//page 1
session_start();
$user="dvjnvki";
$_SESSION['user_name_loggedin'] = $user;
header("Location: b.php");
?>`

2 b.php

<?php
//page2
session_start();
if(isset($_SESSION['user_name_loggedin'])){
echo $_SESSION['user_name_loggedin'];
}else{
echo 'not set<br>';
}
?>

and it does work perfectly. So the issue with your code might be that you might not be getting a value for the variable called $user. Try to echo that first and see if you get an output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.