estreetfan999 Posted December 10, 2011 Share Posted December 10, 2011 Hi, Why doesn't this work? Driving me nuts! Thanks. <?php if($_SERVER[php_SELF] == 'somepage.php') { Header("Location: http://www.thissite.com"); } else { Header("Location: http://www.anothersite.com"); } ?> The code NEVER redirects to the first URL (thissite.com). It ALWAYS redirects to the second URL (anothersite.com). What the heck am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/252865-redirect-from-header-with-conditional-statement/ Share on other sites More sharing options...
xyph Posted December 10, 2011 Share Posted December 10, 2011 First, you need to use quotes around strings. Since associative arrays use strings for keys, you need quotes around the key 'PHP_SELF'. Second, have you echo'd $_SERVER['PHP_SELF'] to verify it contains what you expect? It probably doesn't contain 'somepage.php', otherwise it would execute the first header() call. Quote Link to comment https://forums.phpfreaks.com/topic/252865-redirect-from-header-with-conditional-statement/#findComment-1296449 Share on other sites More sharing options...
estreetfan999 Posted December 10, 2011 Author Share Posted December 10, 2011 Thanks. I'm using an include file, which has this redirect code in it. <?php require_once('include-test.php'); ?> Placed this include file at the very top of somepage.php I added the quotes around 'PHP_SELF' Still going to second link and ignoring the first link. Quote Link to comment https://forums.phpfreaks.com/topic/252865-redirect-from-header-with-conditional-statement/#findComment-1296451 Share on other sites More sharing options...
trq Posted December 10, 2011 Share Posted December 10, 2011 Did you echo $_SERVER['PHP_SELF'] to see what it actually contains? Quote Link to comment https://forums.phpfreaks.com/topic/252865-redirect-from-header-with-conditional-statement/#findComment-1296452 Share on other sites More sharing options...
estreetfan999 Posted December 10, 2011 Author Share Posted December 10, 2011 Yes. Just did. Thanks so much. That was the answer. ($_SERVER[php_SELF] == 'somepage.php') was actually going to /solutions/somepage.php (that's my file structure) It all works now. Really appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/252865-redirect-from-header-with-conditional-statement/#findComment-1296460 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.