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
Share on other sites

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)

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.