SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Thread: php redirection

  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php redirection

    Dear All,

    I have been searching a way to redirect a page to another page without avail for example:

    Code:
    if($password == $pwd)
    {
       //ok
    }
    else
    {
       redirect to loginproblem.php
    }
    I cannot use header('Location, loginproblem.php')
    beacuse the if statement and all the code associated with this is located withim the html tags.

    Is there any solutions to this?

    Regards
    p_h_p

  2. #2
    SitePoint Enthusiast pento's Avatar
    Join Date
    Jan 2006
    Location
    Russia, Novosibirsk
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by p_h_p
    Dear All,
    ...
    I cannot use header('Location, loginproblem.php')
    beacuse the if statement and all the code associated with this is located withim the html tags.
    ..
    Can you explain this?
    GetDown->MakeLove();

  3. #3
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you mean redirection without seeing vars in the address, you can use Curl
    http://www.zend.com/pecl/tutorials/curl.php

    I was asking about that for secure redirection here
    http://www.sitepoint.com/forums/showthread.php?t=309942
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

  4. #4
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pento:

    technically i can use header('location, url') but I would prefer not to.

    the reason for not doing this is because:

    Code:
    if($password == $pwd)
    {
       echo "You are logged in";
       //more text would be outputted as well
    }
    else
    {
       redirect to loginproblem.php
    }
    I can not add the text to the page if this if else statement occures before the html tags. I understand I could just do someting like:
    Code:
    if($pwderr)
    {
       header ('location, loginproblem.php');
    }
    
    <html>
    ....
    
    if($password == $pwd)
    {
       echo "You are logged in";
       //more text would be outputted as well
    }
    else
    {
       $pwderr;
       refresh - how can you refresh without using the header.
    }
    
    </html>

  5. #5
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What the hell? That's confusing. What are you trying to achieve?

    Going on the last bit of code you showed, then you could use the HTML equivilent of a refresh.

    Code:
    if($pwderr)
    {
       header ('location, loginproblem.php');
    }
    
    <html>
    ....
    
    if($password == $pwd)
    {
       echo "You are logged in";
       //more text would be outputted as well
    }
    else
    {
       $pwderr;
       ?><meta http-equiv="refresh" content="1;url=http://somewebsitehere.com"><?
    }
    
    </html>
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  6. #6
    SitePoint Enthusiast pento's Avatar
    Join Date
    Jan 2006
    Location
    Russia, Novosibirsk
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    p_h_p
    Don't mix PHP and HTML!
    And auth check you must do before all html output.
    Make file like auth.php and include it in all files..
    In this file do auth check.
    GetDown->MakeLove();

  7. #7
    SitePoint Enthusiast pento's Avatar
    Join Date
    Jan 2006
    Location
    Russia, Novosibirsk
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > refresh - how can you refresh without using the header.
    with JavaScript
    GetDown->MakeLove();

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use a javascript redirect, although i cant remember the syntax. its going to fail if a user doesnt have js enabled though.

    a headers is usually the best way to do it.

  9. #9
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if($pwderr)
    {
       header ('location, loginproblem.php');
    }
    
    <html>
    ....
    
    if($password == $pwd)
    {
       echo "You are logged in";
       //more text would be outputted as well
    }
    else
    {
       $pwderr;
       ?><meta http-equiv="refresh" content="1;url=http://somewebsitehere.com"><?
    }
    
    </html>
    What's wrong with HTML refresh?
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  10. #10
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd say your only options here, unless you want to use a js redirect, would be to enable output buffering, or, clean up your code and don't mix php and html.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  11. #11
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    from what I understand the html refresh won't work unless its in in <head></head> tags which is why F4nat1c the refresh will not work.

    Never mind guys I have worked a solution around my original query.

    Thanks for the help.

    p_h_p

  12. #12
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by p_h_p
    from what I understand the html refresh won't work unless its in in <head></head> tags which is why F4nat1c the refresh will not work.

    Never mind guys I have worked a solution around my original query.

    Thanks for the help.

    p_h_p
    That's a load of BS. I've used the HTML refresh in the middle of a script many a time, where the header() function was not feasible.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  13. #13
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    F4nat1c: But it's not exactly ideal if you're trying to have valid html/xhtml.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  14. #14
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    F4nat1c
    I was under the assumption that all meta tags had to be located within the <head></head> tags, and through my research of the refresh I was after I came across this functionaility you stated but the sites I read stated it had to be located within the <head></head tags which I had assumed anyway.

    Thanks for pointing this out, but I was under the knwoledge this would not work.

    Thanks for everyones help but I must have been asleep earlier when I posted this query (lol) as I coded the plain obvious and it works.

    Regards
    p_h_p

  15. #15
    SitePoint Zealot
    Join Date
    Apr 2005
    Location
    Helsingborg, Sweden
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you try ob_start() as devbanana pointed out? That would make you able to use header("location: url").

  16. #16
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The obvious way to do this is to do the checks before any output is sent. If a redirect is needed then it is easy to pass any required parameters through the URL, or even by establishing session variable.

    There are very few occasions where a header-type function needs to occur inline with the HTML.

    Remember this - PHP code is processed BEFORE any output is sent to the browser. Any inline PHP code should only be for display info/logic; any other type of action should be coded before the <html> tag.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

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