Page 1 of 1

how to redirect one page to another page

Posted: Sat Jan 26, 2008 12:03 am
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.

Re: how to redirect one page to another page

Posted: Sat Jan 26, 2008 12:06 am
by DonPatrizio
you can redirect a page with html. why php?

Re: how to redirect one page to another page

Posted: Sat Jan 26, 2008 12:17 am
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

Re: how to redirect one page to another page

Posted: Sat Jan 26, 2008 12:23 am
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.

Re: how to redirect one page to another page

Posted: Sat Jan 26, 2008 4:42 am
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.

Re: how to redirect one page to another page

Posted: Sat Jan 26, 2008 11:10 am
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");

Re: how to redirect one page to another page

Posted: Sat Jan 26, 2008 11:27 am
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;

Re: how to redirect one page to another page

Posted: Sun Jan 27, 2008 6:35 am
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?

Re: how to redirect one page to another page

Posted: Sun Jan 27, 2008 9:32 am
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

Re: how to redirect one page to another page

Posted: Sun Jan 27, 2008 10:07 am
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

Re: how to redirect one page to another page

Posted: Sun Jan 27, 2008 6:13 pm
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" :)

Re: how to redirect one page to another page

Posted: Mon Jan 28, 2008 2:09 am
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');