Jump to content


Photo

Conditional Redirection


  • Please log in to reply
3 replies to this topic

#1 matts_sandbox

matts_sandbox
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 April 2006 - 06:01 PM

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?


#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 15 April 2006 - 11:59 PM

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:

// 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;


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


#3 matts_sandbox

matts_sandbox
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 April 2006 - 01:08 AM

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!

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 16 April 2006 - 01:26 AM

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:
...
$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'";
...

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users