Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-25-2008, 10:33 PM   PM User | #1
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
If User Hasn't Logged In Don't Show Page

I am editing a site where the person has set it up so even though pages can't be viewed, there is no session checking... This is what is currently is there...

PHP Code:
<?php 
    
if($_SESSION['session_started'] != "TRUE")
    
session_start();
?>
I changed it to the following but it just keeps re-directing to the welcome.htm page...

PHP Code:
<?php 
    
if($_SESSION['user_logged'] != "TRUE")
    
header('Location: welcome.htm');
?>
tomyknoker is offline   Reply With Quote
Old 02-25-2008, 10:44 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
The $_SESSION global array won't exist unless you have session_start() at the top of the script.
__________________
Fumigator is offline   Reply With Quote
Old 02-25-2008, 10:45 PM   PM User | #3
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
So like so:

PHP Code:
<?php  
    
if($_SESSION['user_logged'] != "TRUE")
    
session_start(); 
    
header('Location: welcome.htm'); 
?>
tomyknoker is offline   Reply With Quote
Old 02-25-2008, 10:48 PM   PM User | #4
Andrew Johnson
Banned

 
Join Date: Feb 2008
Location: Winnipeg, Canada
Posts: 396
Thanks: 0
Thanked 29 Times in 29 Posts
Andrew Johnson can only hope to improve
No, like this:

PHP Code:
<?php  
    session_start
(); 
    if(
$_SESSION['user_logged'] != "TRUE")
    
header('Location: welcome.htm'); 
?>
session_start(); should always be the first thing you do, unless of course one of your sessions in an object, then you need to define that object's class first - but I doubt you'll have to worry about that for quite some time
Andrew Johnson is offline   Reply With Quote
Old 02-25-2008, 10:54 PM   PM User | #5
meth
Regular Coder

 
meth's Avatar
 
Join Date: Jan 2003
Posts: 262
Thanks: 0
Thanked 9 Times in 9 Posts
meth is on a distinguished road
Try:
PHP Code:

<?php 
    
if(!isset($_SESSION)) session_start();
    if(!
$_SESSION['user_logged']) header('Location: welcome.htm');    
?>
Of course this will only work providing that your logon script declares the var $_SESSION['user_logged'] with a boolean value properly. If the user has logged, $_SESSION['user_logged'] = TRUE; else $_SESSION['user_logged'] = FALSE.

Check your logon script...
__________________
I do Web Design, Brisbane based.
More time spent in PHP/MySQL Web Development.
And Search Engine Optimisation takes up the rest of it.
meth is offline   Reply With Quote
Old 02-25-2008, 11:44 PM   PM User | #6
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Quote:
Originally Posted by meth View Post
Try:
PHP Code:

<?php 
    
if(!isset($_SESSION)) session_start();
    if(!
$_SESSION['user_logged']) header('Location: welcome.htm');    
?>
Of course this will only work providing that your logon script declares the var $_SESSION['user_logged'] with a boolean value properly. If the user has logged, $_SESSION['user_logged'] = TRUE; else $_SESSION['user_logged'] = FALSE.

Check your logon script...
That first "if" statement will never be "false", so you may as well just code session_start();.
__________________
Fumigator is offline   Reply With Quote
Old 02-26-2008, 04:35 AM   PM User | #7
meth
Regular Coder

 
meth's Avatar
 
Join Date: Jan 2003
Posts: 262
Thanks: 0
Thanked 9 Times in 9 Posts
meth is on a distinguished road
PHP Code:
<?php
$session_msg 
= (!isset($_SESSION)) ? 'session not set''session set';
echo 
$session_msg;
?>
output = session not set

PHP Code:
<?php
session_start
();
$session_msg = (!isset($_SESSION)) ? 'session not set''session set';
echo 
$session_msg;
?>
output = session set


session_start() always returns true. The test isset($_SESSION) can go either way.
__________________
I do Web Design, Brisbane based.
More time spent in PHP/MySQL Web Development.
And Search Engine Optimisation takes up the rest of it.
meth is offline   Reply With Quote
Old 02-26-2008, 06:08 AM   PM User | #8
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
I took issue with checking isset($_SESSION) before the session_start() function was called (which seems to be what the OP is getting hung up on), that's all
__________________
Fumigator is offline   Reply With Quote
Old 02-26-2008, 06:16 AM   PM User | #9
meth
Regular Coder

 
meth's Avatar
 
Join Date: Jan 2003
Posts: 262
Thanks: 0
Thanked 9 Times in 9 Posts
meth is on a distinguished road
and if you "just use session_start()", you'll cause another error if it has already been called in an include; hence it's safer to call session_start() after the test for $_SESSION being set. Best to provide solutions that wont introduce new errors.
__________________
I do Web Design, Brisbane based.
More time spent in PHP/MySQL Web Development.
And Search Engine Optimisation takes up the rest of it.
meth is offline   Reply With Quote
Old 02-26-2008, 06:20 AM   PM User | #10
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
I guess if your includes are unknown black boxes, that's valid. Personally I keep an eye on what I include...
__________________
Fumigator is offline   Reply With Quote
Old 02-26-2008, 06:30 AM   PM User | #11
meth
Regular Coder

 
meth's Avatar
 
Join Date: Jan 2003
Posts: 262
Thanks: 0
Thanked 9 Times in 9 Posts
meth is on a distinguished road
Personally I don't give a toss about your includes. I'd prefer not to make assumptions about the questioner's entire site based on a 3 line snippet.
__________________
I do Web Design, Brisbane based.
More time spent in PHP/MySQL Web Development.
And Search Engine Optimisation takes up the rest of it.
meth is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 PM.


Advertisement
Log in to turn off these ads.