Jump to content

Archived

This topic is now archived and is closed to further replies.

timgetback

how to redirect with php.

Recommended Posts

I want to create a php code that redirects a user to a different website. I searched through the forums so i wouldnt haveto posta new topic but i didnt find any thing if anyone could help it would be greatly appreciated.

Share this post


Link to post
Share on other sites
so what it should look like:

[code]<?php
header("Location: http://www.example.com");
#then the rest of the code? cause i wanted more code there.
?>
[/code]

Share this post


Link to post
Share on other sites
Depends on what else you want to do..

Just remember that you cannot send any output to the browser prior to the header() and anything after won't be seen because its not on that page anymore.

As for it continuing execution of the script after the header() function, I am not sure if it does any of it.  I usually have exit() after my header() and then whatever else I need to do will be in the script that you're calling header with.

Share this post


Link to post
Share on other sites
You can put the header anywhere in the php code as long as the "<?PHP" is the first thing on the page... anything before the header will be done anything after likely wont....

Share this post


Link to post
Share on other sites
i got another question i have this on my login page:

[code]if ((!$username) || (!password)) {
    include("error.html");
    exit;
}[/code]

but every time i type in a username and password it still brings me to the error page regarless of whether or not i type in the correct information.

Share this post


Link to post
Share on other sites
thanks... also when i added the header function at the end of my php file.... it kept telling me that i have an error at line 47  which is the line that has "?>" i dont understand why theres an error there.   When i moved the header to the top of my php file and added "exit;" right after it it repeated the same error.

Share this post


Link to post
Share on other sites
Could you post the full error message here, along with your PHP code.

Share this post


Link to post
Share on other sites
headers need to be at the top, see the sticky post about headers
You can use ob_start(); on line 1, but its not the best way

Share this post


Link to post
Share on other sites
[quote author=timgetback link=topic=102866.msg408949#msg408949 date=1154620190]
thanks... also when i added the header function at the end of my php file.... it kept telling me that i have an error at line 47  which is the line that has "?>" i dont understand why theres an error there.  When i moved the header to the top of my php file and added "exit;" right after it it repeated the same error.
[/quote]

Might be you're missing a ending } somewhere in your code.

Share this post


Link to post
Share on other sites
Pehaps the best way to do a redirect would be to echo an javascript...

Share this post


Link to post
Share on other sites
im not familiar with java script... but heres my code:

[code]
<?php
session_start();
include 'db2.php';
$username = $_POST['username'];
$passwd = $_POST['password'];

$username = stripslashes($username);
$passwd = stripslashes($passwd);

if ((!$username) || (!$password)) {
    include("error.html");
    exit;
}
 
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (username, password)
        VALUES('$username', '$passwd')") or die (mysql_error());

$result = mysql_query($sql,$connection) or die (mysql_error());

header("location: http://www.domain.com");
    exit;
  ?>[/code]

I was thinking of adding "include 'redirect.php'; " and having the header there instead.....

Share this post


Link to post
Share on other sites
if ((!$username) || (!$password)) {

Should be-

if ((!$username) || (!$passwd)) {

Rest seems fine :)

Orio.

Share this post


Link to post
Share on other sites
header location, for some reason doesn't always have to be at the top, there are a lot of times I use header location to change people over to a page based on them doing something.  I might ahve a bunch of scripts already ran, or running on the page, includes, and calculations, and everythin gelse, then in the middle of the page, have 1 header location based on if they do something wrong, it still seems to work no matter where you put the redirect at.

Share this post


Link to post
Share on other sites
You can place header, sessiojn_start, mail, setcookie or any other function that changes the headers anywhere within your script, aslong as there is no output before the use of these functions.

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=102866.msg409040#msg409040 date=1154626491]
You can place header, sessiojn_start, mail, setcookie or any other function that changes the headers anywhere within your script, aslong as there is no output before the use of these functions.
[/quote]

mail? :)

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=102866.msg409040#msg409040 date=1154626491]
You can place header, sessiojn_start, mail, setcookie or any other function that changes the headers anywhere within your script, aslong as there is no output before the use of these functions.
[/quote]

what???? so i cant have all my php scripts before the header function? Cause if you check i have it at the very end.. which casused problems....

Share this post


Link to post
Share on other sites
if you have header somewhere other than the top
you need ob_start(); at the top of the page

Share this post


Link to post
Share on other sites
[quote author=onlyican link=topic=102866.msg409056#msg409056 date=1154627214]
if you have header somewhere other than the top
you need ob_start(); at the top of the page

[/quote]

If there is some output sent to the browser that is...

Share this post


Link to post
Share on other sites
[quote author=Ifa link=topic=102866.msg409045#msg409045 date=1154626925]
[quote author=wildteen88 link=topic=102866.msg409040#msg409040 date=1154626491]
You can place header, sessiojn_start, mail, setcookie or any other function that changes the headers anywhere within your script, aslong as there is no output before the use of these functions.
[/quote]

mail? :)
[/quote]
Yes believe it or not mail does send headers.

[quote author=onlyican link=topic=102866.msg409056#msg409056 date=1154627214]
if you have header somewhere other than the top
you need ob_start(); at the top of the page

[/quote]
You'll want to add ob_start(); at the top of each PHP page and ob_end_flush(); at the end of each PHP page.

Share this post


Link to post
Share on other sites
But I'm using mail in a place where cookie/session and all that stuff would not work. But this wonderful piece of letters works :) And the mail even gets delivered  8)

Share this post


Link to post
Share on other sites
I'm not sure with that one then, however I remember reading something about mail and headers in an online article.

Share this post


Link to post
Share on other sites
Well yeah, there is headers that is sent with the mail, but they are not really the same thing...

Share this post


Link to post
Share on other sites
echo "

<SCRIPT LANGUAGE='JavaScript'>

window.location='rederict.php';



</script>

";

thats what i use or you can use..

print "<META HTTP-EQUIV=Refresh CONTENT=\"10;url=rederict.php\">";

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.