talbot_brian Posted June 22, 2011 Share Posted June 22, 2011 Hello, I am trying to take a URL which is entered into the browser and then use strpos to redirect to specific pages based on the referrer. The idea is that anyone typing "blue" or some derivative thereof into the browser is redirected to bluepage.html and all other visitors are redirected to otherpage.html However, everyone is redirected to otherpage.html, including those who type "blue" into the browser. Why? Thanks! <?php $ref = $_SERVER['HTTP_REFERER']; # have also tried HTTP_POST if (strpos($ref, "blue")) { header('Location: bluepage.html'); exit; # have also tried without the 'exit;' } require('otherpage.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240108-strpos-problem/ Share on other sites More sharing options...
bh Posted June 22, 2011 Share Posted June 22, 2011 hi, The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. so becouse not all user agent set this value, you have to check whether this property is exists or not. try to debug it (eg output the referer) Quote Link to comment https://forums.phpfreaks.com/topic/240108-strpos-problem/#findComment-1233328 Share on other sites More sharing options...
cyberRobot Posted June 22, 2011 Share Posted June 22, 2011 You might want to review the manual for strpos(): http://php.net/manual/en/function.strpos.php The function may return results which will evaluate to false, such as 0 if "blue" is found at the beginning of the string. Maybe try something like: <?php $ref = $_SERVER['HTTP_REFERER']; # have also tried HTTP_POST if (strpos($ref, "blue") !== false) { header('Location: bluepage.html'); exit(); # have also tried without the 'exit;' } require('otherpage.html'); ?> Note that the code is untested. Quote Link to comment https://forums.phpfreaks.com/topic/240108-strpos-problem/#findComment-1233337 Share on other sites More sharing options...
silkfire Posted June 22, 2011 Share Posted June 22, 2011 Even better, use stripos to allow for entering "bLuE" with mixed case. Quote Link to comment https://forums.phpfreaks.com/topic/240108-strpos-problem/#findComment-1233344 Share on other sites More sharing options...
Pikachu2000 Posted June 22, 2011 Share Posted June 22, 2011 HTTP_REFERER: The address of the page (if any) which referred the user agent to the current page . . . Try using $_SERVER['REQUEST_URI'] and see if that gives you the result you're looking for. if( stripos( $_SERVER['REQUEST_URI'], 'blue' ) !== FALSE ) { // etc. } Quote Link to comment https://forums.phpfreaks.com/topic/240108-strpos-problem/#findComment-1233400 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.