tags:

views:

37

answers:

4

Hello,

The code below is supposed to redirect the user to the index file if the user ($uid) has submitted an entry in the last hour. It's not working.

Any idea why not?

Thanks in advance,

John

$queryuidcount = "select loginid from submission where datesubmitted > (NOW() - INTERVAL 1 hours) AND loginid = '$uid'"; 
$uidresult = mysql_query($queryuidcount);

if (mysql_num_rows($uidresult) >= 1)
{

   session_write_close();
   header("Location:http://www.domain.com/sample/index.php");
   exit;

}
A: 

Does the code even reach the code inside the if-statement? try a var_dump("foo"); there, because I reckon the header looks good. It could be that the exit; is interfering with the redirect though. Good luck!

Robbert
+4  A: 

First of all, there should be a space between Location: and http://someurl, otherwise it's not a correctly formed HTTP header (some browsers can cope, some choke on it):

Location: http://someurl

Second, are you getting a "headers already sent" warning? That would mean you already started output before this line (e.g. whitespace at file beginning, UTF BOM marks, etc.)

Piskvor
+2  A: 

Looks like you have forgot to put session_start() on top of your script since you are using a sessio related function below. Also, no need to specify whole domain path, just the file name should be fine:

header("Location: index.php");

If there is any error, you can know about it by putting these lines on top of your script:

ini_set('display_errors', true);
error_reporting(E_ALL);

Also make sure that some records are returned:

if (mysql_num_rows($uidresult) >= 1)
{
   exit('found rows !!');
}
Sarfraz
'Also, no need to specify whole domain path, just the file name should be fine:' This is WRONG. Although most browsers accept it and do as intended, this is against the specs - RFC2616.
Pete
A: 

The SQL code doesn't seem correct. Change the hours to hour instead.

(NOW() - INTERVAL 1 hour)

Perhaps you could you use TIMEDIFF instead?

mysql> select TIMEDIFF('2010-07-07 09:38:28',NOW()); 
+---------------------------------------+
| TIMEDIFF('2010-07-07 09:38:28',NOW()) |
+---------------------------------------+
| -01:03:38                             |
+---------------------------------------+
1 row in set (0.00 sec)
John P