0

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.

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

so? Registration page use the same configuration and queries is done immediately. May be it's because of sessions?
echo return 180. It means that session will expire after 180 minutes?
probably seconds, or 3 minutes, but the documentation isn't clear. Usually those settings are in seconds, though.
1
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.

1 Comment

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

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.

4 Comments

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
Changing ID's manually is kind of a hassle isn't it? See my edits.
No problem. Oh, also make sure that id is set to auto-increment.
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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.