Jump to content

Conditional Redirection


matts_sandbox

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?
Link to comment
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).
Link to comment
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!
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.