Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Simple PHP question

    How do I use PHP code to redirect the user to:

    (1)a different file at the same web address
    (2)a different HTTP address altogether

    I thought it was something using the "header" function but don't remember exactly what to do.

    Thank You

    CGG

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    header('Location: http://site.com/page.ext'); 
    Location is the header. Technically it requires a full URL in order to redirect, but most of the time it will accept a relative url to the page you want to redirect to. It assumes your site if it cannot find a fully qualified url.

    Also, add an exit(); after the header unless you want the page to continue processing. These are generally used in if/else type block checks, so you only redirect if the user meets some criteria so it makes sense to add the exit anyway.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Fou-Lu,

    Thank You. Unfortunately it gave me this error message:

    Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web226/b2264/sl.garryplaza/public_html/delete_maint_req.php:6) in /hermes/bosweb/web226/b2264/sl.garryplaza/public_html/delete_maint_req.php on line 62

    CGG

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You can't set header information once content has been displayed (echo/print/whatever).

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    So how would I redirect a user to a different page AFTER I have displayed information on the current page?

    Thx

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    But WHY do you need to do it after you display information? Can you post your code please? Almost always you don't need to send a header after you display information, headers are sent normally before any output.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by cgeller100 View Post
    So how would I redirect a user to a different page AFTER I have displayed information on the current page?
    you cannot. it’s that simple.

    Edit: you could use the HTML refresh meta tag, though that still requires to be printed before the HTML body
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Regular Coder olidenia's Avatar
    Join Date
    Oct 2009
    Location
    Sitting In Front Of A Screen
    Posts
    110
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Is this what you are looking for?

    You can show the page and then redirect after x time, if you put it anyware in the page it will work.

    To adjust time edit the numbers: setTimeout('Redirect()',1000);

    for example: setTimeout('Redirect()',500);

    Or redirect instantly: setTimeout('Redirect()',0);

    Edit url to go to: location.href = 'redirect.html';


    Code:
    <?php
    echo "<script type=\"text/javascript\"> setTimeout('Redirect()',1000); function Redirect(){location.href = 'redirect.html';}</script>";
    ?>
    But ofcourse this is not PHP just Javascript echoed by PHP
    Last edited by olidenia; 10-13-2010 at 03:47 PM.

  • Users who have thanked olidenia for this post:

    cgeller100 (10-14-2010)

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by olidenia View Post
    But ofcourse this is not PHP just Javascript echoed by PHP
    and it only works, if JavaScript is enabled …
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    New Coder
    Join Date
    Jan 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    This is Exactly what I was looking for - didn't think of using Javascript rather than PHP. Thanks!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •