Jump to content

Strpos problem.


talbot_brian

Recommended Posts

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');

 

?>

Link to comment
https://forums.phpfreaks.com/topic/240108-strpos-problem/
Share on other sites

hi,

 

 

  Quote
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)

Link to comment
https://forums.phpfreaks.com/topic/240108-strpos-problem/#findComment-1233328
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/240108-strpos-problem/#findComment-1233337
Share on other sites

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.
}

Link to comment
https://forums.phpfreaks.com/topic/240108-strpos-problem/#findComment-1233400
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.