ramone_johnny Posted May 20, 2013 Share Posted May 20, 2013 (edited) 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? Edited May 20, 2013 by ramone_johnny Quote Link to comment Share on other sites More sharing options...
Eiseth Posted May 20, 2013 Share Posted May 20, 2013 (edited) 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() Edited May 20, 2013 by Eiseth Quote Link to comment 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" /> Quote Link to comment Share on other sites More sharing options...
Eiseth Posted May 20, 2013 Share Posted May 20, 2013 (edited) $_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; Edited May 20, 2013 by Eiseth Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Eiseth Posted May 20, 2013 Share Posted May 20, 2013 (edited) 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 Edited May 20, 2013 by Eiseth Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.