PHPiSean Posted August 20, 2011 Share Posted August 20, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/ Share on other sites More sharing options...
3raser Posted August 20, 2011 Share Posted August 20, 2011 Why would you need to do that? If they choose they want to be remembered, just use a cookie; If not, then just simply set a session. Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/#findComment-1259670 Share on other sites More sharing options...
PHPiSean Posted August 20, 2011 Author Share Posted August 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/#findComment-1259674 Share on other sites More sharing options...
3raser Posted August 20, 2011 Share Posted August 20, 2011 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('', '', ''); ?> Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/#findComment-1259676 Share on other sites More sharing options...
PHPiSean Posted August 20, 2011 Author Share Posted August 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/#findComment-1259688 Share on other sites More sharing options...
Psycho Posted August 20, 2011 Share Posted August 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/#findComment-1259691 Share on other sites More sharing options...
PHPiSean Posted August 20, 2011 Author Share Posted August 20, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/#findComment-1259976 Share on other sites More sharing options...
PHPiSean Posted August 21, 2011 Author Share Posted August 21, 2011 Anyone have an idea? Quote Link to comment https://forums.phpfreaks.com/topic/245259-question-regarding-sessions-and-cookies/#findComment-1260024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.