Browser redirect based on user client help needed...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluefrog
Forum Newbie
Posts: 3
Joined: Sat Jun 13, 2009 5:04 am

Browser redirect based on user client help needed...

Post by bluefrog »

Please skip down to post number 4 thanks :)

Hello everybody, I'm not a coder so please treat me like an idiot and put everything into the simplest terms you can.

Basically I have a php page on my site which is designed to redirect to another location based on the url of the surfer:

i.e.
So as you can see it works on a magic number.

But when I use it without the magic number everything works fine, but when used with the magic number it just sits there trying to load a page. I've tried changing the location to google and still the same so I assume it's a code or server problem.

Any help would be appreciated:

Code: Select all

<?php
 
//Settings
 
$magic_number = 1; // Any number you choose except ZERO!
 
$cpa_offer_url = 'http://www.google.com';
 
$red_method = 0; // 0 = JS Form, 1 = JS, 2 = Meta Refresh
 
$form_method = 0;  // 0 = POST, 1 = GET
 
 
//Don't edit below this line unless you know what you are doing.
 
if (isset($_GET['mn']) && $_GET['mn']==$magic_number){
 
        echo '<html><head></head><body><form action="' . 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'] . '" method="post" id="form1">
 
<input type="hidden"  name="mn" value="' . $magic_number . '" /></form>
 
<script language="JavaScript">
    document.getElementById(\'form1\').submit();</script></body></html>';
        return true;
        exit();
}
 
if ($_POST['mn']==$magic_number){
 
        if($red_method==0) {
 
            if($form_method==0)
                $formmethod = "POST";
            else
                $formmethod = "GET";
 
            echo '<html><head></head><body><form action="' . $cpa_offer_url .  '" method="' . $formmethod . '" id="form1"></form>
<script language="JavaScript">
 
    document.getElementById(\'form1\').submit();
 
</script></body></html>';
        }
 
            else if($red_method == 1)
 
    echo    '<HEAD>
<SCRIPT language="JavaScript">
<!--
window.location="' . $cpa_offer_url . '";
//-->
</SCRIPT>
</HEAD>';
 
        else echo '<meta http-equiv="refresh" content=0;url=' . $cpa_offer_url  . '>';
 
 
 
    exit();
}
 
?>
 
 
<html>
  <head>
    <title>page 1</title>
  </head>
  <body>
    <br><br><br><br>
     <center><h1>page 1</h1></center>
 
  </body>
</html>
 
Or if anybody knows of some code which will complete this task which works?

Thank you all in advance.
Last edited by bluefrog on Sun Jun 14, 2009 2:47 pm, edited 1 time in total.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: Server not responding

Post by jazz090 »

if you could tell us a little bit more about the logic of this page, whats a magic number etc... it would help. also omitting braces in logics is very unprofessional and u will be surprised how often the errors come from bad but "correct" syntax. also instead os tracing a javascript to redirect, just use this php syntax:

Code: Select all

header("Location: ".$cpa_offer_url);
die();
also you seem to have endless errors in syntax and for some reason your mixing up JS and PHP without seperating each code via <?php ?>. your JS code also has errors in syntax within itself.
bluefrog
Forum Newbie
Posts: 3
Joined: Sat Jun 13, 2009 5:04 am

Re: Server not responding

Post by bluefrog »

It's not my code, like I say I'm not a coder and have no idea on most of what you just said. lol.

The magic number is in the script as 1 but can be any number except 0, this is just an identifier so that when the url is typed or clicked, if it ends in ?mn='magicnumber' it redirects to it's final location, if not then it redirects to the default location as specified in the latter half of the script.

I actually specified javascript redirect, I prefer it over php as in the past I've had errors and probelms with php redirection so now, either out of superstition or convenience I still use javascript. Plus the page is javascript heavy so at least I know the visitor will be able to use the features.

In addition I would like to place some extra code in there which checks to see if the browser is opera or google chrome and if either of them automatically use the default location as specified in the bottom of the script.

If you run the script on a server or local environment you will see how it works, very simple in operation as there are only two outcomes; either redirect based on url input or default to location.

Is there any software I can use to check for these errors and syntax problems?
bluefrog
Forum Newbie
Posts: 3
Joined: Sat Jun 13, 2009 5:04 am

Re: Server not responding

Post by bluefrog »

OK I've managed to cobble something together which works, but now I would like to add user client redirect as in it checks the user client FIRST and gives a static location (straight url) if that client is matched, this is for Google chrome and Opera browsers.

The code that works is:

Code: Select all

<?php
 
 
 
// USEAGE //
 
// Your bh marketing uses http://www.yourdomain.com/scriptlocation.php which will redirect to the alt location
// Your wh url is http://www.yourdomain.com/scriptlocatio ... agicnumber (ie URL/test.php?p=1234) which will redirect once
 
 
 
$magicnumber = '1234';  // Any number except zero
 
$wh = 'www.google.com'; //normal redirect (with magicnumber)
 
$bh = 'www.yahoo.com'; //alternative redirect
 
 
 
 
 
// DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING //
 
 
if (isset($_GET['p']) && $_GET['p']==$magicnumber)
{
 
echo    '<HEAD>
<SCRIPT language="JavaScript">
<!--
window.location="http://' . $wh . '";
//-->
</SCRIPT>
</HEAD>';
 
        return true;
        exit();
 
 
}
 
else
 
{
 
echo    '<HEAD>
<SCRIPT language="JavaScript">
<!--
window.location="http://' . $bh . '";
//-->
</SCRIPT>
</HEAD>';
 
        return true;
        exit();
 
}
 
?>
Anybody know how I could do this so it checks the browser BEFORE it decides if the magic number was right or not, and only if none of the browser types are found does it check where to send based on the magicnumber input.

Thanks in advance guys :)
Post Reply