Jump to content

Archived

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

matts_sandbox

Conditional Redirection

Recommended Posts

The following PHP code used to work perfectly:

/*verify registration*/

$sql_user_check = "select * from firstmysqltable where email='$email'";
$result_name_check = mysql_query($sql_user_check);
$usersfound = mysql_num_rows($result_name_check);

// if user not found, note that and end

if ($usersfound < 1)
header("Location: [a href=\"http://www.site.com/stopit.html");\" target=\"_blank\"]http://www.site.com/stopit.html");[/a]

// if user does exist, continue with processing

else
header("Location: [a href=\"http://www.site.com/goahead.html");\" target=\"_blank\"]http://www.site.com/goahead.html");[/a]

Then I attempted to “enhance” the process:

/*verify registration*/

$sql_user_check = "select * from mysecondtable where email='$email' and secondcolumn='on'";
$result_name_check = mysql_query($sql_user_check);
$usersfound = mysql_num_rows($result_name_check);

if ($usersfound < 1)
header("Location: [a href=\"http://www.site.com/stopit.html");\" target=\"_blank\"]http://www.site.com/stopit.html");[/a]

// if user does exist, continue with processing

else
header("Location: [a href=\"http://www.site.com/goahead.html");\" target=\"_blank\"]http://www.site.com/goahead.html");[/a]

Now, everything goes to stopit.html. What am I missing?

Share this post


Link to post
Share on other sites
Always check for any MySQL errors before executing any subsequent MySQL commands. Check that the query works before running the number of rows function. You may have a syntax error in your SQL. Do something like this to help yourself debug the problem:

[code]
// Add to make sure you see PHP errors/warnings/notices
ini_set('display_errors', '1');
error_reporting(E_ALL);

/*verify registration*/

$sql_user_check = "select * from mysecondtable where email='$email' and secondcolumn='on'";
$result_name_check = mysql_query($sql_user_check);
if (!$result_name_check) {
    // Add to debug
    echo "SQL: $sql_user_check Error: ", mysql_error();
    exit;  // Stop script to fix error
}
$usersfound = mysql_num_rows($result_name_check);

if ($usersfound < 1)
header("Location: http://www.site.com/stopit.html");

// if user does exist, continue with processing

else
header("Location: http://www.site.com/goahead.html");

exit;
[/code]


Please note that the header() with location command does not redirect right there and then when it's executed. It actually redirects when your script ends or an exit/die is reached. So, to ensure no logic flow problems in your script, you should have an exit right after every header() with location to force redirection to occur immediately (if that's what you want/expect).

Share this post


Link to post
Share on other sites
I very, very much appreciate the reply, but it still doesn't work. I've checked, and the MySQL works fine. I think it's something I don't understand about the attempted redirection. In other words:

$usersfound = 1 BUT in all instances, the redirection is to stopit.html

Help, I've been trying to fix this for something like 3 hours, and I'm gonna bust!

Share this post


Link to post
Share on other sites
Well, it's not finding a match in the table with whatever the value of $email is (and the secondcolumn having to be set to "on").

Where is $email coming from or being assigned?

if it's passed from a form through GET method use $_GET['email'] and if it's through POST use $_POST['email']. Example:
[code]
...
$email = ('get' == strtolower($_SERVER['REQUEST_METHOD'])) ?  $_GET['email'] : $_POST['email'];

$email = get_magic_quotes_gpc() ? $email : addslashes($email);

$sql_user_check = "select * from `mysecondtable` where `email` = '$email' and `secondcolumn` = 'on'";
...
[/code]

Start putting displays (echo/print) at strategic places within your code to help yourself debug this.

Try the SQL query in the mysql command line, phpmyadmin or similar tool to see what you get.

If this doesn't help you, then you need to provide more info and the complete exact code you're using.

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.