Jump to content

Question regarding sessions and cookies


PHPiSean

Recommended Posts

Hello,

 

On my site I offer the option an option for cookies or sessions on login. If a remember me box is selected, then a cookie will be set. My question is, how do I assign both the $_SESSION['id'] and $_COOKIE['id'] to the same variable?

 

Thanks for the help!

 

 

Link to comment
Share on other sites

For this reason. I have no idea which one they set. Therefore, if I run a query such as

"select * from users where userid='$_SESSION['id']'"

that wouldn't include the cookie I don't believe. For this, when the users view their profiles, I can run one loop where both the session and cookie are used in the loop's query to gather the respected information.

Link to comment
Share on other sites

For this reason. I have no idea which one they set. Therefore, if I run a query such as

"select * from users where userid='$_SESSION['id']'"

that wouldn't include the cookie I don't believe. For this, when the users view their profiles, I can run one loop where both the session and cookie are used in the loop's query to gather the respected information.

 

Can't you just checked if they selected the "Remember Me" box on the login page? After you check that, you could just do something like such:

 

<?php
//the rememebr me checkbox
//assuming you're using the post function
//change remember_me to your field name
$stay_logged_in = $_POST['remember_me'];

if(empty($stay_logged_in)) ? $username = $_SESSION['sessionname'] : $username = setcookie('', '', '');

?>

Link to comment
Share on other sites

I think you and I are on a different page. After I set my cookie or session, there is no problem. The problem I see is when I am creating the user profile page so that they can edit their information. For example, I want to run a query to get information about a user's posts, in doing so, I will either use the variable $_SESSION['id'] or $_COOKIE['id'], (which ever is registered) to run a query to select the needed information of their posts from a the posts database. Now, instead of creating separate:

 

<?php

if(isset($_SESSION['id'])) {
run loop
}

if(isset($_COOKIE['id'])){
run loop
}

?>

 

How do I make that just one?

Link to comment
Share on other sites

Always set the session regardless of whether the user wants to be "remembered" or not. I assume you have a login?

 

When the user first hits your site you should check if the cookie value is set. If so, authenticate against that cookie value and then set the session value. If no, then have the user log in and set the session (and also set the cookie if they choose to be remembered). While the user is on your site always use the session value.

Link to comment
Share on other sites

Always set the session regardless of whether the user wants to be "remembered" or not. I assume you have a login?

 

When the user first hits your site you should check if the cookie value is set. If so, authenticate against that cookie value and then set the session value. If no, then have the user log in and set the session (and also set the cookie if they choose to be remembered). While the user is on your site always use the session value.

 

How should I go about doing this? In my header file, is it fine if I continually use an if statement to see if a cookie is set and to keep setting the session as the user browses the site, for example.

 


<?php

if (!isset($_COOKIE['id'])||!isset($_SESSION['id'])){
echo "<li><a href='register.php'>Register</a></li>";
echo "</ul>";
  echo "
    <table>  
    <tr>
    <form name='login' method='post' action='login.php'>
    <tr><td>Username</td> <td><input type='text' name='username'></td></tr>
    <tr><td>Password</td> <td><input type='password' name='password'></td></tr>
    <tr><td><input type='submit' name='submit' value='Login'></td></tr>
    <tr><td><input type='checkbox' name='rememberme' value='remember'> Remember</td></tr>
    </form>
    </table>
";
    
}else{
    echo "</ul>";
    
    if(isset($_COOKIE['id'])) {
        $_SESSION['id'] = $_COOKIE['id'];
    }
    //USERBOX
}

?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.