PDA

View Full Version : redirect user after email is found in SQL DB



hey_suburbia
August 1st, 2006, 06:42 PM
I want to redirect a user to a "logged in" page after their email is found to exist in my database.

I'm using PHP and mySQL.

Here is what I have:

<?php


mysql_connect("localhost","user","pass");


mysql_select_db("db");

$email = $_POST['email'];

$fetch_exist=mysql_query("SELECT email FROM db WHERE email = '$email'");

if(mysql_num_rows($fetch_exist)>0)
{
header('Location: www.site.com/page1.html');
exit;
}

else
{
$error_message .="
That Email is not in our database. \n
Please register here: www.site.com/page2.html
";
die ($error_message);
}

?>

The connection works fine because I was able to get a text output on both, it's the redirection script that causes errors:

header('Location: http://www.google.com');
exit;

It says that the headers have been sent and normally I would know how to fix this (just put the "location" script at the very top of the page and that's it.

Anyone know how I would do this?

What has to happen:

If email exists, user goes to page1.html.
If email doesn't exist, user goes to page2.html

bwh2
August 1st, 2006, 07:05 PM
hmm. try removing the exit;

hl
August 1st, 2006, 07:11 PM
The exit; won't change it whether you have it there or not.

It's probably something with the query, are you sure you're not inserting it into the database name rather than the table name?

edit:/ I felt like making some cleaner looking code :P No I didn't test this.


<?
@mysql_connect("localhost", "user", "pass") or die("MySQL connections can be *censor*'s sometimes...");
@mysql_select_db("db") or die("Silly database! Silly damn database! Wake up!");
if(isset($_POST['email'])){
$email = $_POST['email'];
$query = "SELECT * FROM `db` WHERE `email`='" . $email . "'";
$result = @mysql_query($query) or die("DIE PHP DIE!");
if(mysql_num_rows($result) !== 1){
die("Email not in database. Go register.");
}
else{
header('Location: woot.html');
}
}
else{
die("H4x0rz! You didn't input an email when coming here dude!");
}
?>

bwh2
August 1st, 2006, 07:13 PM
yeah, i think harish is right. i didn't bother to look at the table name in the query.

hey_suburbia
August 2nd, 2006, 10:45 AM
The thing is that if I just put in some text to be displayed on both the if and the else statements I'll get the text no problem.

The problem occurs when I try to redirect, I get:

Warning: Cannot modify header information - headers already sent by (output started at /usr/com/site/public_html/login.php:3) in /usr/com/site/public_html/login.php on line 23


Line 23 is the header('Location: http://www.site.com');

hey_suburbia
August 2nd, 2006, 05:00 PM
*solved*

My original code was correct EXCEPT I had an extra line space at the end.

I can't believe it was that simple!

hl
August 2nd, 2006, 06:41 PM
Haha.