tags:

views:

66

answers:

4

Hello. I'm using mysql and php for registration on my web-site. Registration is ok. Mysql do queries immediately. But in login there strange things begin to happen. I insert a test in my php code to insert test row to database. First test code inserted immediately, but 2nd was inserted after series of refresh and relog actions only after 10 minutes. The 3rd test query is the same-after approximately 10 minutes after 2nd query.

Here is login code:

<?php
session_start();
if(isset($_SESSION['id'])){
        echo 'You have logged in.';
        echo $_SESSION['id'];
    } 
  else {    
        $email=$_POST['email'];
        $password=$_POST['password'];
        $db=new mysqli('','','','');  
        if (mysqli_connect_errno()) {
            echo 'Unable to connect to database: '.mysqli_connect_error().'. Please e-  mail our system administrator. We will fix this error as soon as possible. Thanks for patience and understanding. ';
            exit();
        }
//TEST QUERY
            $query="insert into test values(3, 'test')";
            $result=$db->query($query);
//LOGIN QUERY
        $query="select id from users where email='$email' and password='$password'";   
        $result=$db->query($query);   
        if ($result->num_rows==0) {
            echo 'Incorrect email or password.';               
        }  
        else {
            $row=$result->fetch_assoc();
            $_SESSION['id']=$row['id'];
            echo 'You have logged in.';
            echo $_SESSION['id'];
//THIS TEST QUERY IS NOT IMPLEMENTED
            $query="insert into test values(1, test)";
            $result=$db->query($query);
        }  
        }   
?>

Where is mistake?

Test table consists of 2 columns: id (medium int, primary key, unsigned) and test(text)

Thanks in advance.

+1  A: 

Sounds like the cookie could be expiring after 10 minutes. Run echo session_cache_expire(); to see what your expiration time is set to. More details at http://php.net/manual/en/ref.session.php

Matt Williamson
so? Registration page use the same configuration and queries is done immediately. May be it's because of sessions?
dsplatonov
echo return 180. It means that session will expire after 180 minutes?
dsplatonov
probably seconds, or 3 minutes, but the documentation isn't clear. Usually those settings are in seconds, though.
Hans
+1  A: 

This looks suspicious to me.

$query="insert into test values(3, 'test')";

Is it trying to set the ID of every row inserted to 3? ID's have gotta be unique.

EDIT:

This probably won't fix your problem, but it will make your life easier by not forcing you to manually change ID's each time.

INSERT INTO test SET <colname>='test'

where <colname> is the name of the column that "test" is going into.

DLH
yeah, right. I changed code every try. When was 1st try there was "1", then i change id to 2 and refresh the page. Query was not implemented :( only after approximately 10 minutes
dsplatonov
Changing ID's manually is kind of a hassle isn't it? See my edits.
DLH
ok - will change. Thanks
dsplatonov
No problem. Oh, also make sure that id is set to auto-increment.
DLH
And make sure both of your `INSERT` queries are the same. I think your second `INSERT` query may have been causing problems, as Hans suggested.
DLH
+1  A: 
insert into test values(1, test)

-- test -- needs quotes or you are going to get an error that the column test doesn't exist (unless it does). If it does exist, it's probably going to be empty, as mysql probably doesn't know what you mean by test -- maybe your version thinks it's a constant or something.

If you posted what the table structure of your test table is, that would help solve it probably.

Hans
yeah - in my IDE there was quotes. Test table consists of 2 columns: id (medium int, primary key, unsigned) and test(text).
dsplatonov
+1  A: 

Just a little security hint: your SQL queries are very dangerous as they are prone to SQL injection attacks. See the Wikipedia article for alternatives ...

Marius Schulz
May have been better as a comment, but a good point.
DLH
ok will change, but this didn't solve my problem
dsplatonov