dombrorj Posted April 27, 2011 Share Posted April 27, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/ Share on other sites More sharing options...
gizmola Posted April 27, 2011 Share Posted April 27, 2011 Here's what I have, but doesn't seem to be entirely working... This is not a helpful description of your problem. Exactly what works and what doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/#findComment-1207135 Share on other sites More sharing options...
requinix Posted April 27, 2011 Share Posted April 27, 2011 1. You can't have multiple values for $find. Check each one, one at a time. 2. Use strpos instead of preg_match(). Quote Link to comment https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/#findComment-1207136 Share on other sites More sharing options...
KevinM1 Posted April 27, 2011 Share Posted April 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/#findComment-1207141 Share on other sites More sharing options...
dombrorj Posted April 28, 2011 Author Share Posted April 28, 2011 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/#findComment-1207283 Share on other sites More sharing options...
requinix Posted April 28, 2011 Share Posted April 28, 2011 Don't use stristr(). If you need it case-insensitive then use stripos. Quote Link to comment https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/#findComment-1207286 Share on other sites More sharing options...
dombrorj Posted April 28, 2011 Author Share Posted April 28, 2011 Don't use stristr(). If you need it case-insensitive then use stripos. Ok, thanks for catching that. Quote Link to comment https://forums.phpfreaks.com/topic/234894-http-referrer-redirect-not-working/#findComment-1207292 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.