Web Design and Web Development Forum

  1. #1
    cancer10's Avatar
    Join Date
    Jun 2005
    Location
    India
    Age
    29
    Posts
    498
    Rep Power
    7
  2. cancer10 is on a distinguished road
  3. Question URL redirection in PHP

    Is there any other function in PHP that can be used for redirecting a page

    I use the following

    Code:
    header ("location: index.php");

    But it generates the following error:

    Code:
    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at c:\program files\easyphp1-8\www\admin-right-nav.php:5) in c:\program files\easyphp1-8\www\admin-right-nav.php on line 6

    Is there any alternative in php?
    Reply With Quote Reply With Quote
  4. #2
    Join Date
    Jul 2005
    Location
    Derbyshire, UK
    Age
    20
    Posts
    2,144
    Rep Power
    9
  5. sirjavabean is on a distinguished road
  6. Re: URL redirection in PHP

    You need to send any headers before any other text is sent to the browser. Someone else can proably explain this better to you. In a nutshell, any
    Code:
    header()
    functions need to be used before any
    Code:
    echo()
    or similar functions.
    Reply With Quote Reply With Quote
  7. #3
    awalsh's Avatar
    Join Date
    Oct 2005
    Location
    127.0.0.1
    Posts
    120
    Rep Power
    7
  8. awalsh is on a distinguished road
  9. Re: URL redirection in PHP

    Or could use Output buffering so you can keep your code as it is (its better coding practice in my opinion to "fix" the script though, just showing how you could get around it).

    To use output buffering (the whole script is processed then output sent to the browser so you can use header() and echo() etc anywhere in the script) add the following to the top of the script (just below the <?php)

    PHP Code:
    ob_start(); 
    then at the end of the script (just before the ?>) you should add:

    PHP Code:
    ob_end_flush(); 
    But generally its better to fix the script instead of using output buffering in my opinion. Im sure if you post the code i or others could easily help you to fix it.

    Actually on the other hand if you want to use a different redirect method try googling for Meta Redirect. Which you can use with a simple echo() or print().
    Last edited by awalsh; 04-28-2006 at 06:41 AM.
    O/S's | Debian 3.1 (2.6 Kernel) | Gentoo (2.4 kernel) | Slackware 10.2 (2.4 kernel) | Fedora 4 (2.6 kernel)

    My development site
    Reply With Quote Reply With Quote
  10. #4
    Join Date
    Dec 2005
    Posts
    1,819
    Rep Power
    8
  11. Umang is on a distinguished road
  12. Re: URL redirection in PHP

    Quote awalsh originally posted:
    Actually on the other hand if you want to use a different redirect method try googling for Meta Redirect. Which you can use with a simple echo() or print().
    that wont be easy coz if u already had echo()ed the <body> section then u cant echo something back in the <head> section. ob_start(); ob_end_flush(); is better. u can use output buffering for cookies also.
    I'm off to sigcont.com
    Reply With Quote Reply With Quote
  13. #5
    awalsh's Avatar
    Join Date
    Oct 2005
    Location
    127.0.0.1
    Posts
    120
    Rep Power
    7
  14. awalsh is on a distinguished road
  15. Re: URL redirection in PHP

    Its not ideal to put a meta redirect in the <body> section because its not really good (x)html but it does work as far as i know. In firefox it works because i used it a few mins ago in a quick alteration i did for a friend (a little temporary raffle script, seemed pretty pointless writing a perfect redirection system for it)
    O/S's | Debian 3.1 (2.6 Kernel) | Gentoo (2.4 kernel) | Slackware 10.2 (2.4 kernel) | Fedora 4 (2.6 kernel)

    My development site
    Reply With Quote Reply With Quote
  16. #6
    Join Date
    Dec 2005
    Posts
    1,819
    Rep Power
    8
  17. Umang is on a distinguished road
  18. Re: URL redirection in PHP

    but then output buffering doesnt have any disadvantage does it?
    or you could do it manualy. but this will not take into acount error. you can set all the output data in variables and the echo() them at the end of a page. in case of an error it wil not be possible to legally(as in XHTML standards) redirect. so output buffering is the best and if u dont want to do that store everything in variables and echo the head then body sections in order at the end of the php page. i hope i'm clear
    for eg
    PHP Code:
    if(something=="correct"){
    $head=."something";
    }
    else{
    $body.="something";

    Last edited by Umang; 04-28-2006 at 10:01 AM.
    I'm off to sigcont.com
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Mar 2006
    Location
    Toronto, Ontario
    Posts
    2,270
    Rep Power
    9
  20. Nick Presta is on a distinguished road
  21. Re: URL redirection in PHP

    You need to send headers before any output.

    You can get around this with output buffering but I don't suggest it as I think it's stupid the end user has to wait because you like to send headers where ever in the script.

    Post your script so we can help you.
    Reply With Quote Reply With Quote
  22. #8
    cancer10's Avatar
    Join Date
    Jun 2005
    Location
    India
    Age
    29
    Posts
    498
    Rep Power
    7
  23. cancer10 is on a distinguished road
  24. Re: URL redirection in PHP

    Actually this is my code

    Code:
    <?
    session_start();
    
    echo "Logging out ".$_SESSION['userid'];
    
    header("location: tussu.php");
    
    ?>

    I get the error

    Code:
    Logging out admin
    Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\logout.php:3) in c:\program files\easyphp1-8\www\logout.php on line 6
    Reply With Quote Reply With Quote
  25. #9
    Join Date
    Oct 2004
    Age
    20
    Posts
    1,967
    Rep Power
    9
  26. Reaper is on a distinguished road
  27. Re: URL redirection in PHP

    PHP Code:
    <?

    header
    ("location: tussu.php");

    session_start();

    echo 
    "Logging out ".$_SESSION['userid'];

    ?>
    Reply With Quote Reply With Quote
  28. #10
    unclekyky's Avatar
    Join Date
    Sep 2004
    Age
    22
    Posts
    5,184
    Rep Power
    13
  29. unclekyky is on a distinguished road
  30. Re: URL redirection in PHP

    Quote Reaper originally posted:
    PHP Code:
    <?

    header
    ("location: tussu.php");

    session_start();

    echo 
    "Logging out ".$_SESSION['userid'];

    ?>
    Haha, if you do that then the session_start() and the 'echo "Logg..."' is pointless.

    I recommend just using javascript for a redirect, if you want some echoed to the page first.
    Spore-Game - The Ultimate Spore Fan-Site
    Abnegating Avunculicide Since 1601 | YC Wiki - "Quidquid latine dictum sit, altum sonatur."
    Reply With Quote Reply With Quote

Similar Threads

  1. PHP Tutorial for beginners
    By YoungCoder in forum PHP Articles
    Replies: 19
    Last Post: 04-23-2006, 02:31 PM
  2. What is PHP?
    By wizard in forum PHP Articles
    Replies: 4
    Last Post: 11-27-2005, 06:38 AM
  3. Php Syntax
    By wizard in forum PHP Articles
    Replies: 1
    Last Post: 10-06-2005, 01:53 AM
  4. PHP Manual [Chris's Version]
    By Chrishasfun in forum PHP Articles
    Replies: 0
    Last Post: 01-02-2005, 02:22 PM