timgetback Posted August 3, 2006 Share Posted August 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/ Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 header("Location: http://www.example.com");First line Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68562 Share on other sites More sharing options...
timgetback Posted August 3, 2006 Author Share Posted August 3, 2006 so what it should look like:[code]<?phpheader("Location: http://www.example.com");#then the rest of the code? cause i wanted more code there.?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68580 Share on other sites More sharing options...
SharkBait Posted August 3, 2006 Share Posted August 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68584 Share on other sites More sharing options...
legohead6 Posted August 3, 2006 Share Posted August 3, 2006 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.... Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68590 Share on other sites More sharing options...
timgetback Posted August 3, 2006 Author Share Posted August 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68592 Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 your missing the $ in password Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68607 Share on other sites More sharing options...
timgetback Posted August 3, 2006 Author Share Posted August 3, 2006 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 Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68617 Share on other sites More sharing options...
wildteen88 Posted August 3, 2006 Share Posted August 3, 2006 Could you post the full error message here, along with your PHP code. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68648 Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 headers need to be at the top, see the sticky post about headersYou can use ob_start(); on line 1, but its not the best way Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68650 Share on other sites More sharing options...
SharkBait Posted August 3, 2006 Share Posted August 3, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68657 Share on other sites More sharing options...
Ifa Posted August 3, 2006 Share Posted August 3, 2006 Pehaps the best way to do a redirect would be to echo an javascript... Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68658 Share on other sites More sharing options...
timgetback Posted August 3, 2006 Author Share Posted August 3, 2006 im not familiar with java script... but heres my code:[code]<?phpsession_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..... Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68674 Share on other sites More sharing options...
Orio Posted August 3, 2006 Share Posted August 3, 2006 if ((!$username) || (!$password)) {Should be-if ((!$username) || (!$passwd)) {Rest seems fine :)Orio. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68681 Share on other sites More sharing options...
Ninjakreborn Posted August 3, 2006 Share Posted August 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68701 Share on other sites More sharing options...
wildteen88 Posted August 3, 2006 Share Posted August 3, 2006 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 Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68706 Share on other sites More sharing options...
Ifa Posted August 3, 2006 Share Posted August 3, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68711 Share on other sites More sharing options...
timgetback Posted August 3, 2006 Author Share Posted August 3, 2006 [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.... Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68717 Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 if you have header somewhere other than the topyou need ob_start(); at the top of the page Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68722 Share on other sites More sharing options...
Ifa Posted August 3, 2006 Share Posted August 3, 2006 [quote author=onlyican link=topic=102866.msg409056#msg409056 date=1154627214]if you have header somewhere other than the topyou need ob_start(); at the top of the page[/quote]If there is some output sent to the browser that is... Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68726 Share on other sites More sharing options...
wildteen88 Posted August 3, 2006 Share Posted August 3, 2006 [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 topyou 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. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68735 Share on other sites More sharing options...
Ifa Posted August 3, 2006 Share Posted August 3, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68737 Share on other sites More sharing options...
wildteen88 Posted August 3, 2006 Share Posted August 3, 2006 I'm not sure with that one then, however I remember reading something about mail and headers in an online article. Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68739 Share on other sites More sharing options...
Ifa Posted August 3, 2006 Share Posted August 3, 2006 Well yeah, there is headers that is sent with the mail, but they are not really the same thing... Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68741 Share on other sites More sharing options...
Dobakat Posted August 3, 2006 Share Posted August 3, 2006 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\">"; Quote Link to comment https://forums.phpfreaks.com/topic/16451-how-to-redirect-with-php/#findComment-68750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.