Jump to content


Photo

how to redirect with php.


  • Please log in to reply
41 replies to this topic

#1 timgetback

timgetback
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 03 August 2006 - 02:26 PM

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.

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 02:27 PM

header("Location: http://www.example.com");
First line
Tell me the problem, I will try tell you the solution

#3 timgetback

timgetback
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 03 August 2006 - 02:51 PM

so what it should look like:

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


#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 03 August 2006 - 03:02 PM

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.



#5 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 03 August 2006 - 03:07 PM

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....
"Syntax error" .. WHERE THE **** IS IT?!

#6 timgetback

timgetback
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 03 August 2006 - 03:11 PM

i got another question i have this on my login page:

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

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.

#7 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 03:40 PM

your missing the $ in password
Tell me the problem, I will try tell you the solution

#8 timgetback

timgetback
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 03 August 2006 - 03:49 PM

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.

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 August 2006 - 04:33 PM

Could you post the full error message here, along with your PHP code.

#10 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 04:35 PM

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
Tell me the problem, I will try tell you the solution

#11 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 03 August 2006 - 04:41 PM

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.


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


#12 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 03 August 2006 - 04:42 PM

Pehaps the best way to do a redirect would be to echo an javascript...
I'm just a guy who likes to code for fun...

#13 timgetback

timgetback
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 03 August 2006 - 05:00 PM

im not familiar with java script... but heres my 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;
  ?>

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

#14 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 03 August 2006 - 05:08 PM

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

Should be-

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

Rest seems fine :)

Orio.
Think you're smarty?

(Gone until 20 to November)

#15 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 03 August 2006 - 05:30 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#16 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 August 2006 - 05:34 PM

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.

#17 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 03 August 2006 - 05:42 PM

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.


mail? :)
I'm just a guy who likes to code for fun...

#18 timgetback

timgetback
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 03 August 2006 - 05:45 PM

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.


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....

#19 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 05:46 PM

if you have header somewhere other than the top
you need ob_start(); at the top of the page

Tell me the problem, I will try tell you the solution

#20 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 03 August 2006 - 05:48 PM

if you have header somewhere other than the top
you need ob_start(); at the top of the page


If there is some output sent to the browser that is...
I'm just a guy who likes to code for fun...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users