Redirect in "else"

Discussion in 'PHP' started by randomlande, Jul 11, 2006.

  1. #1
    i have an if/else statement and i want the page to redirect to another page in the else part (i tired header, it doesnt seem to work).
     
    randomlande, Jul 11, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    OK, thanks for letting us know...

    :) how about you post the code? Or do you think we can magically re-create and fix what you have there?
     
    T0PS3O, Jul 11, 2006 IP
  3. randomlande

    randomlande Peon

    Messages:
    118
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well is there some other way to redirect besides using header.

    its as simple as this:
    
    <?php
    
    if (file_exists("images/$c.jpg")) {
         echo "<img src='images/$c.jpg' border=1>";
    }
    else
    {
    
    echo "<br>No more<br><a href=index.php><b>Click here</b></a> to go home or <a href=c.php?c=1><b>click here</b></a> to go back to the first image";
    
    }
    
    
    ?>
    PHP:
    I want to redirect to c.php?c=1
     
    randomlande, Jul 11, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can't echo anything before redirecting. Anything echoed after redirecting, obviously won't be shown. So drop the text and redirecting via header should work. Otherwise try a meta refresh redirect.
     
    T0PS3O, Jul 11, 2006 IP
  5. randomlande

    randomlande Peon

    Messages:
    118
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i tried replacing the whole echo part with a header redirect and said it can't modify header info.

    can meta refresh be put in the body?
     
    randomlande, Jul 11, 2006 IP
  6. randomlande

    randomlande Peon

    Messages:
    118
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    actually i figured it out, by making the user not even be able to get to the page where there are no more images
     
    randomlande, Jul 11, 2006 IP
  7. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #7
    just make sure absolutely NOTHING is sent to the browser prior to the redirect. Meta refresh works in almost all cases, can be fast, and anything can be sent to the browser prior to redirect. Can also try a javascript redirect (document.location=....)
     
    ccoonen, Jul 11, 2006 IP
  8. SeanMck

    SeanMck Peon

    Messages:
    122
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well you could always send the headers BEFORE the echo, but put a 5 second delay on the headers and then the echo text will show up for 5 seconds before you are re-directed.
     
    SeanMck, Jul 11, 2006 IP
  9. ahref

    ahref Well-Known Member

    Messages:
    1,123
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    170
    #9
    use
    header("location:filename.htm");
    or
    echo '<script>location.href="filename.htm"</script>';
     
    ahref, Jul 11, 2006 IP