elmas156 Posted February 18, 2011 Share Posted February 18, 2011 I'm trying to clean up my code as best I know how and get rid of code that isn't necessary. This is where I'm most confused: On every page, I start a session and if someone goes to a page other than the index/login page, they are redirected if there is no session, otherwise, they are allowed to continue to view the page content. Here is my code: <?php session_start(); $email=$_SESSION['email']; // is this line necessary on every page... if ($_SESSION['logged'] != 1) { $_SESSION['email'] = $email; // as well as this line? header("Location: index.php"); exit(); } ?> Basically, I need to know if the SESSION['email'] has to be set on every page, or does it stay set as long is the session is logged? If it stays set for the entire session, I shouldn't need to include the "$email=$_SESSION['email'];" line on every page, right? Also, if my thinking is correct, I shouldn't need to include the "$_SESSION['email'] = $email;" line unless I need to use the $email variable for some other purpose. Am I correct on this? Thanks for any input. Link to comment https://forums.phpfreaks.com/topic/228101-help-with-cleaning-up-my-code/ Share on other sites More sharing options...
MatthewJ Posted February 18, 2011 Share Posted February 18, 2011 No, you only need to set a session variable once for the life of the session. FOr the last question, I'm guessing you just have it backward, should be $email = $_SESSION['email'];. Either way, you don't even really need to do that, just use it as $_SESSION['email']. Link to comment https://forums.phpfreaks.com/topic/228101-help-with-cleaning-up-my-code/#findComment-1176251 Share on other sites More sharing options...
KevinM1 Posted February 18, 2011 Share Posted February 18, 2011 No, you only need to set a session variable once for the life of the session. So long as the OP isn't changing the value of the variable. Something like: $email = $_SESSION['email']; $email = "My cat's breath smells like cat food."; Won't automatically update the session's version of the email. Seems obvious, but I've seen others have issues like that in the past. Link to comment https://forums.phpfreaks.com/topic/228101-help-with-cleaning-up-my-code/#findComment-1176253 Share on other sites More sharing options...
elmas156 Posted February 18, 2011 Author Share Posted February 18, 2011 OK, so after the session variable is set using this line: $_SESSION['email'] = $email; I do not need to include this on any pages that user will view while they are signed in... as long as the value of $email doesn't change? So would this make more sense to include on all pages where I am using $email for other purposes: session_start(); if ($_SESSION['logged'] == 1) { $email=$_SESSION['email']; } else { header("Location: index.php"); exit(); } Of course, this is only after the session variable has been set using this line when the user logs in: $_SESSION['email'] = $email; Anyone see any potential problems with this? Thanks again for your help. Link to comment https://forums.phpfreaks.com/topic/228101-help-with-cleaning-up-my-code/#findComment-1176263 Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 one problem. if $_SESSION['logged'] isn't set, you'll get an undefined index error. i would modify one line as follows: if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) { Link to comment https://forums.phpfreaks.com/topic/228101-help-with-cleaning-up-my-code/#findComment-1176275 Share on other sites More sharing options...
elmas156 Posted February 18, 2011 Author Share Posted February 18, 2011 Thanks very much. I'll give it a try. Link to comment https://forums.phpfreaks.com/topic/228101-help-with-cleaning-up-my-code/#findComment-1176300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.