Redirect Based on IF Statement

Discussion in 'PHP' started by craigedmonds, Aug 20, 2006.

  1. #1
    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.
     
    craigedmonds, Aug 20, 2006 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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...
     
    TwistMyArm, Aug 20, 2006 IP
  3. craigedmonds

    craigedmonds Notable Member

    Messages:
    705
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    235
    #3
    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?
     
    craigedmonds, Aug 20, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    T0PS3O, Aug 20, 2006 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 .
     
    TwistMyArm, Aug 20, 2006 IP
  6. jroxonline

    jroxonline Peon

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this:

    <?php
    $powref='SANLUCAR';
    $powurl='sanlucar.php';

    if (isset($_REQUEST['ref']))
    {
    $ref=$_REQUEST['ref'];
    if ($powref==$ref)
    {
    header('Location: '. $powurl);
    }
    }
    ?>
     
    jroxonline, Aug 20, 2006 IP
  7. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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):
     
    clancey, Aug 20, 2006 IP
  8. craigedmonds

    craigedmonds Notable Member

    Messages:
    705
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    235
    #8
    craigedmonds, Aug 20, 2006 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Just use $_GET['ref'].

    
    <?php
    $powref = 'SANLUCAR';
    $powurl = 'sanlucar.php';
    $ref = $_GET['ref'];
    
    if ($powref == $ref){
    header('Location: http://www.brecknellproperties.com/' . $powurl);
    }
    ?>
    
    PHP:
     
    T0PS3O, Aug 20, 2006 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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, Aug 20, 2006 IP
  11. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #11
    TwistMyArm is right, the location has to be with the http and all not relative. See my updated code above.
     
    T0PS3O, Aug 20, 2006 IP
  12. craigedmonds

    craigedmonds Notable Member

    Messages:
    705
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    235
    #12
    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?
     
    craigedmonds, Aug 20, 2006 IP
  13. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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");
     
    clancey, Aug 20, 2006 IP
  14. craigedmonds

    craigedmonds Notable Member

    Messages:
    705
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    235
    #14
    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.
     
    craigedmonds, Aug 20, 2006 IP