Jump to content

Recommended Posts

I'm trying to redirect visitors that contain broadly matched URLs. For example, redirect the visitor if their url contains: "/welcome" or "order.php" or "login.mysite/"

 

At the same time I also want to redirect the user if they a particular User Agent.

 

Here's what I have, but doesn't seem to be entirely working...

 

<?php

//detect referrer
$ref = $_SERVER['HTTP_REFERER'];
$find = "order.php";
$find = "login.mysite/";

//setting the variables
$linux = stripos($_SERVER['HTTP_USER_AGENT'],"Linux");
$aol = stripos($_SERVER['HTTP_USER_AGENT'],"AOL");

//detecting user agent device, os, browser, etc.
if ($linux == true || $aol == true) $redirect = 1;

//detect referrer
elseif (preg_match($ref, $find)) $redirect = 1;

//set redirects
if ($redirect) {	
    $url = "http://www.google.com";
} 
else {
    $url = "http://www.yahoo.com";
}

header("Location: $url");

?>

 

Any help you can offer is appreciated. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/
Share on other sites

You're also not guaranteed that HTTP_REFERER will work, or contain the value you want: http://php.net/manual/en/reserved.variables.server.php

 

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.

1. You can't have multiple values for $find. Check each one, one at a time.

2. Use strpos instead of preg_match().

 

Thanks! I got it to work with your suggestions (code below)

 

You're also not guaranteed that HTTP_REFERER will work, or contain the value you want: http://php.net/manual/en/reserved.variables.server.php

 

Yeah, I know its not perfect but it'll have to do. Thanks for pointing me to that.

 

Thanks for the help! Here's what I ended up with and seems to be working...

 

<?php

//detect referrer
$ref = $_SERVER['HTTP_REFERER'];

//setting the user agent variables
$linux = stripos($_SERVER['HTTP_USER_AGENT'],"Linux");
$sample = stripos($_SERVER['HTTP_USER_AGENT'],"SAMPLE");

//detecting user agent device, os, browser, etc.
if ($linux == true || $sample == true) $redirect = 1;

//detect referrer
if (stristr($ref , "order.php")) $redirect = 1;
if (stristr($ref , "login.mysite")) $redirect = 1;


//set redirects
if ($redirect) {	
    $url = "http://www.google.com";
} 
else {
    $url = "http://www.yahoo.com";
}

header("Location: $url");

?>

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.