The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
$_SESSION issues
Discuss $_SESSION issues in the PHP Development forum on Dev Shed. $_SESSION issues PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 3rd, 2013, 11:03 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 296
  
Time spent in forums: 3 Days 8 h 42 m 14 sec
Reputation Power: 5
|
|
|
$_SESSION issues
Apparently I must not be thinking right today. I made a login, which set 2 session variables, and I cannot get the to echo when called later. And the issue I am havin now is checking if a user is logged in and, if not, redirecting them to the login page. Anyone see something wrong with this?
PHP Code:
<?php
session_start();
if(!isset($_SESSION['loggedin']) && $_GET['loc'] != 'login') {
header('index.php?loc=login');
}
?>
EDIT: The variable $_SESSION['loggedin'] can not be set, or even a blank string, because this is the only reference that exists of it so far.
Last edited by Triple_Nothing : February 3rd, 2013 at 11:12 AM.
|

February 3rd, 2013, 11:15 AM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
Quote: | Originally Posted by Triple_Nothing
PHP Code:
header('index.php?loc=login');
|
In order to redirect you have to send the Location: header.
Code:
header('Location: index.php?loc=login');
|

February 3rd, 2013, 11:28 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 296
  
Time spent in forums: 3 Days 8 h 42 m 14 sec
Reputation Power: 5
|
|
Thank you very much. I so missed that. Now, as far as my echo to such. Just to make sure it is set, I altered the login check to:
PHP Code:
<?php
session_start();
if(!isset($_SESSION['user']) && $_GET['loc'] != 'login') {
header('Location: index.php?loc=login');
}
?>
And it continues, telling me $_SESSION['user'] is set. Now, a little down the page I run:
PHP Code:
Logged in as: <? echo $_SESSION['user']; ?><br />
via an include(); file, but get no username to show. Any ideas?
|

February 3rd, 2013, 11:31 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 296
  
Time spent in forums: 3 Days 8 h 42 m 14 sec
Reputation Power: 5
|
|
|
Never mind. I just noticed I tossed a short tag in there. Fixed it and all is good. Thanks for the help.
|

February 3rd, 2013, 01:08 PM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
|
Note that after a header() redirect you need to stop the script, such as with exit;. If you do not do that, the script will continue to process and execute whatever else the script does. For example if your page were there to delete something from your DB, it will still delete it even though the user was not logged in and got redirected.
|

February 3rd, 2013, 01:18 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 296
  
Time spent in forums: 3 Days 8 h 42 m 14 sec
Reputation Power: 5
|
|
|
Thanks.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|