how to redirect one page to another page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itarun
Forum Newbie
Posts: 7
Joined: Fri Jan 25, 2008 9:22 am

how to redirect one page to another page

Post by itarun »

hi friends,

i am new guy to php, now i am learning php basic, now i want to know, how to redirect the page from one to another, and i want to know what are other ways we can redirect, please let me know, thanks in advance.
DonPatrizio
Forum Newbie
Posts: 3
Joined: Fri Jan 25, 2008 11:34 pm

Re: how to redirect one page to another page

Post by DonPatrizio »

you can redirect a page with html. why php?
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

Re: how to redirect one page to another page

Post by hannnndy »

hi dude !

you can use this

Code: Select all

header("Location: ../index.php");
but mention that the header syntax should not use after any header sent
i mean any out put any session or etc or even html syntaxes at the top of the page
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how to redirect one page to another page

Post by Chris Corbyn »

DonPatrizio wrote:you can redirect a page with html. why php?
Because to do it with HTML you need to send all of the markup. To do it with PHP you only have to send a HTTP header.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: how to redirect one page to another page

Post by JAM »

Chris Corbyn wrote:
DonPatrizio wrote:you can redirect a page with html. why php?
Because to do it with HTML you need to send all of the markup. To do it with PHP you only have to send a HTTP header.
Also, the meta tags can be ignored using software. In software I mean email-harvesters and personal cURL scripts to mention two. I've seen or written multiple of both.
The header() tho, is a non-skipable thing as it's run directly on the server.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to redirect one page to another page

Post by Christopher »

hannnndy wrote:you can use this

Code: Select all

header("Location: ../index.php");
Remember to use full URLs:

Code: Select all

header("Location: http://mydomain.com/index.php");
(#10850)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: how to redirect one page to another page

Post by JAM »

Yes, as always use th absoluteURI as not all clients accepts the './page.php'-like format. They should if you ask men but noone really does... :wink:
This is pretty useful (credits php.net);

Code: Select all

/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

Re: how to redirect one page to another page

Post by hannnndy »

in asp 3.0 you should only write

Code: Select all

 
response.redirect("url")
but in php there is always this problem i do not know why should not the php solve that using redirect classes i mean writing something like that to solve every thing im realy unhappy :(

why?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how to redirect one page to another page

Post by Chris Corbyn »

hannnndy wrote:in asp 3.0 you should only write

Code: Select all

 
response.redirect("url")
but in php there is always this problem i do not know why should not the php solve that using redirect classes i mean writing something like that to solve every thing im realy unhappy :(

why?
That's barely any different really. If you want ASP syntax then:

Code: Select all

class Response {
  public function redirect($url) {
    header("Location: " . $url);
    exit();
  }
}
$response = new Response();
 
$response->redirect($yourUrl); //same as ASP
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

Re: how to redirect one page to another page

Post by hannnndy »

no that wont be this simple

warnning header already sent
i dont know where and how?
there would happen some header already sent an so and so problems that you wil knock down by it
i meant in the internal classes of the php not our defined ones
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how to redirect one page to another page

Post by Chris Corbyn »

hannnndy wrote:no that wont be this simple

warnning header already sent
i dont know where and how?
there would happen some header already sent an so and so problems that you wil knock down by it
i meant in the internal classes of the php not our defined ones
PHP doesn't really have internal classes as such. It didn't begin life as an object-oriented language so it's all just functions.

The reason you get "headers already sent" is because you're trying to generate output before sending the redirect. The are output buffering functions to help prevent that, but more effectively you should probably be organising your code differently so no output is sent in the first place.

Search around for "headers already sent" and "output buffering" :)
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

Re: how to redirect one page to another page

Post by hannnndy »

we have already searched about it at last we have some functions like

Code: Select all

 
ob_start();
ob_flush();
ob_clear();
 
but is it not a little odd to use them
and
not to use header statement in
functions ,
after html elements ,
in classes

we used that and it leads to the unlucky error of header already send
and the hero of the output buffering


it is not working again this problem :banghead:

Code: Select all

 
            echo("start<br>");
            //print_r(ob_get_status());
            ob_start();
            print_r(ob_get_status());
            
            //echo("test");
            ob_end_clean();
            echo("end<br>");
            
            header('Location: http://www.google.com');
 
Post Reply