ramone_johnny Posted May 20, 2013 Share Posted May 20, 2013 Hey guys, I have the following snippet of code that I'm *trying* to use in order to redirect users based upon a session time out. As I've relatively new to PHP and never written a redirect before, I just wanted to check that it's okay. $_SESSION['start'] = isset($_REQUEST["start"]) ? $_REQUEST["start"] : ""; if (!isset($_SESSION['start'])) { header('Location:http://localhost/advertise/placeanad.php'); session_destroy(); exit(); } The page doesn't redirect, it just sits there - even upon refreshing it. Is there something I'm missing? Do I need to use an !empty statement or something? Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/ Share on other sites More sharing options...
Eiseth Posted May 20, 2013 Share Posted May 20, 2013 Because $_SESSION[''start'] is set to = '' which is TRUE, so the if statement won't run Either use NULL as default value or use the !empty() Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/#findComment-1431108 Share on other sites More sharing options...
ramone_johnny Posted May 20, 2013 Author Share Posted May 20, 2013 Sorry I just realised I was killing the session and then resetting it. My apologies. *doh! I'm not quite sure where I set $_SESSION[''start'] to ''? I'm debugging it to screen and it's showing 1 based upon the previous page. <input type="hidden" name="start" value="1" id="start" /> Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/#findComment-1431109 Share on other sites More sharing options...
Eiseth Posted May 20, 2013 Share Posted May 20, 2013 $_SESSION['start'] = isset($_REQUEST["start"]) ? $_REQUEST["start"] : ""; This $_SESSION['start'] will either had a value of $_REQUEST or a default ""; just use NULL as a default value $_SESSION['start'] = isset($_REQUEST["start"]) ? $_REQUEST["start"] : null; Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/#findComment-1431111 Share on other sites More sharing options...
ramone_johnny Posted May 20, 2013 Author Share Posted May 20, 2013 Yeah I don't really understand what : ""; actually means on the end there. Sorry, working with someone elses code. Is that simply setting a base (default) value if nothing is passed? Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/#findComment-1431112 Share on other sites More sharing options...
Eiseth Posted May 20, 2013 Share Posted May 20, 2013 Yup, it's a shortcut for if else statement. "?" will run if the statement is true or ":" if it isn't Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/#findComment-1431113 Share on other sites More sharing options...
ramone_johnny Posted May 20, 2013 Author Share Posted May 20, 2013 Well this might be a separate question, but I'm not sure I want null values for any of these request variables. Infact, I know I dont. Certainly not in the DB. Is there a better way to write this? $_SESSION['start'] = isset($_REQUEST["start"]) ? $_REQUEST["start"] : ""; Can I strip that stuff off the end and make sure isset($_REQUEST["start"]) actually holds a value? Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/#findComment-1431114 Share on other sites More sharing options...
Eiseth Posted May 20, 2013 Share Posted May 20, 2013 What I mean is for the default value of $_SESSION['start'] not the $_REQUEST['start'] That code has nothing to do with the value of $_REQUEST['start'] We just replace the "" with NULL so that if (!isset($_SESSION['start']) will run if the the value of $_SESSION['start'] is null. Using the "" as a default value of $_SESSION['start'], your if statement will never run becase !isset($_SESSION['start') is always FALSE Link to comment https://forums.phpfreaks.com/topic/278192-redirection-based-upon-session-time-out/#findComment-1431115 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.