Hi, I am exetremely proficient in ASP but I have taken on a PHP site and am trying to make a redirect based on a value posted to a page. Basically the code below, should look at the variable thats sent to it, check to see if it matches the $powref and redirect to $powurl if they are the same. Its not working though for some reason. <?php $powref='SANLUCAR'; $powurl='sanlucar.php'; $ref=$_REQUEST['ref']; if ($powref==$ref){ header('Location: '$powurl); } ?> Can any PHP gurus impres the pants of me by telling me whats not right here? I have spent about 3 hours trying every combination possible and I cant get it to work.
You're missing a period in your header to append the string variable to the rest of the string. Try: header('Location: ' . $powurl); instead. Make sure you've got your error reporting on. This should have shown up as an error...
Thank but still does not work. This is the revised code now: <?php $powref='SANLUCAR'; $powurl='sanlucar.php'; $ref=$_REQUEST['ref']; if ($powref==$ref){ header('Location: ' . $powurl); } ?> I am guessing I need to turn error reporting on via the php.ini file? Any recommended setting?
I don't think $_REQUEST['ref'] means anything in PHP. Try: $_SERVER['HTTP_REFERER'] http://www.expertsrt.com/tutorials/Matt/HTTP_headers.html
Hehe... $_REQUEST['ref'] definitely does mean something in PHP, but you're right: he's probably meaning to use the $_SERVER variable that you suggested. Craig: you can turn error reporting on in php.ini or else call the error_reporting function with the desired level of reporting as per http://php.net/error_reporting .
Try this: <?php $powref='SANLUCAR'; $powurl='sanlucar.php'; if (isset($_REQUEST['ref'])) { $ref=$_REQUEST['ref']; if ($powref==$ref) { header('Location: '. $powurl); } } ?>
I do not know that there is anything wrong with your code beyond what TwistMyArm noticed. The $_REQUEST super global contains the contents of the $_GET, $_PORT and $_COOKIE globals. In order for $_REQUEST['ref'] to have a value, it would need to come from a cookie or a form on a web page. Also, jroxonline makes an important point about testing to see if $_REQUEST['ref'] has a value. Another approach is to see if the reference is empty. if( !empty($_REQUEST['ref']) ) { if( $powref == $_REQUEST['ref'] ) { header('Location: '. $powurl); } } Code (markup):
My Page is being passed this variable: http://www.brecknellproperties.com/property-details.php?ref=SANLUCAR If the ref SANLUCAR exists the page should redirect to /sanlucar.php not http://www.brecknellproperties.com/property-details.php?ref=SANLUCAR. jroxonline's suggestion did not work either, I still just get a blank page. As far as turning on error checking I am lost with that.
Just use $_GET['ref']. <?php $powref = 'SANLUCAR'; $powurl = 'sanlucar.php'; $ref = $_GET['ref']; if ($powref == $ref){ header('Location: http://www.brecknellproperties.com/' . $powurl); } ?> PHP:
Add the line: error_reporting( E_ALL ); to your code. That will turn on all error / warning reports. It could be not working if there is any other output in your code before the header is sent, but at least the error reporting will tell you that. It could also be because of the URL you are redirecting to. I can't remember the requirements for 'Location' headers, so maybe you could try: $powurl='/sanlucar.php'; or even: $powurl='http://yourservername/sanlucar.php';
TwistMyArm is right, the location has to be with the http and all not relative. See my updated code above.
I thank everyone for their help although, it may have been something to do with include files all the time. at the top of my "property-details.php" page I have an include file called "pow.php" (property of the week). The top of the server side code looked kind of like this... <?php require_once('pow.php); // this bit contains my problem code require_once('enquiryList.php'); require_once('cnxBp.php'); ......rest of code blah blah ?> Evry single piece of code I tried in pow.php, simply brought up a blank page. As soon as I moved the problem code and pasted it like this (removing the require_once('pow.php) in the top of the page directly rather than using an include file it worked. <?php $powref = 'SANLUCAR'; $powurl = '/sanlucar.php'; $ref = $_GET['ref']; if ($powref == $ref){ header('Location: ' . $powurl); } ?> <?php require_once('enquiryList.php'); require_once('cnxBp.php'); ......rest of code blah blah ?> Bizarre or what?
Glad to see you found the problem. In answer to your other question, putting something like this at the beginning of a script allows you to redirect all errors to a specific file to help you track down errors in the script and fix them: error_reporting(E_ALL & ~E_NOTICE); ini_set("display_errors", "off"); ini_set("log_errors", "on"); ini_set("error_log", "/home/MY-HOME/php_errors");
Thanks Clancy for the great suggestion regarding posting errors to a file. Thats actually a very good idea. I am making a mental note to do the same with my asp sites. Once again I thank everyone for their suggestions. I now have found the solution and I am really happy.