Jump to content

Recommended Posts

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

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'].

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.