Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php redirects

Status
Not open for further replies.

riffy

Programmer
Mar 29, 2001
106
0
0
US
Hi guys,

How do I redirect a user to another page using PHP?

Header ("Location: "); ???

Arif
 
riffy,

You are on to the right function. All you need is to supply the URL:
Code:
header("Location: [URL unfurl="true"]http://www.myhost.com/myurl/");[/URL]

Keep in mind that the header statement has to be the first thing sent to the browser. So, no echo(), print() etc. before that or it will fail.
 
thanks DR

the following is the code to one of the pages on the site i'm working on:

Code:
<?
include &quot;config.inc&quot;;
$emailcheck = $email;
$sql = &quot;SELECT * FROM information&quot;;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
extract($row);

if ($email == $emailcheck) {
  echo '<META HTTP-EQUIV=&quot;Refresh&quot; Content=&quot;0; URL=literature_new.php&quot;>';
  exit;
}
else {
  $email = $emailcheck;
  echo '<META HTTP-EQUIV=&quot;Refresh&quot; Content=&quot;0; URL=request.php?email=\'$email\'&quot;>';
}
?>

basically what the page is doing is checking to see if the email the user entered is already present in the db, if it is, then the page redirects to literature_new.php otherwise it sets the user-entered email as the email value going to be entered into the db and redirects the user to a form where other information can be filled out

my problem is that even for emails already in the db, the code is not working, it keeps processing the else statement and even then it doesn't carry over the email value

thanks
Arif
 
Arif,

Your problem is not with the forward, it is with checking out the records retrieved from the database.

Right now you only compare it to the first retrieved record. You need to loop through all records that the query result contains:
Code:
while($row = mysql_fetch_array($result)){
   
   # now send the header if they are equal
   if($row['email'] == $emailcheck){
      header(&quot;Location: literature.new.php&quot;);
      exit;
    }
}
# if it hasn't been found in the results use alternate header
header(&quot;Location: request.php?email=&quot;.urlencode($emaicheck));
exit;

That should do the trick.
 
thanks for all your help guys, however i'm still having trouble...
on another page i have this code
Code:
<?
include &quot;config.inc&quot;;
$sql = &quot;SELECT * FROM information&quot;;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
extract($row);

$referer = &quot;[URL unfurl="true"]http://test.philipslighting.com/nam/events/lightfair/request.php?$email&quot;;[/URL]
$ref1 = &quot;[URL unfurl="true"]http://test.philipslighting.com/nam/events/lightfair/check.php&quot;;[/URL]
	
if (($HTTP_REFERER != $referer) || ($HTTP_REFERER != $ref1)) {
  header (&quot;Location: /nam/events/lightfair/checkemail.php&quot;);
  exit;
}
else {
?>

display page

<? } ?>

for some reason, it is not reading my if statement at all...it's not carrying out the != statement as it automatically goes to the else statement even when my referring page is the one i have specified...

any suggestions??

arif
[sad]
[thumbsdown]
 
i know sleipnir

if you are referring to the $HTTP_REFERER variable..it was a typo in my post, i have it as all caps in my code...
 
what do you mean by that? don't we have to set it equal to some value?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top