0

i try to create a little login+survey and try to write all data to a database. Especially it is important, to get a session id for each user in order to add information of each page of the survey to the right line of the database.

My problem is here, that it seems that the session is either not started or i can not create a session id. Writing in the database already works, but not if i include the lines about the session.

After google'ing a lot that always took me to the same answer (which is not working for me) i try it here. Here is my code, first the callDatabase.php is called, in this file a session id is created and the database entry is made (idk if this is the best solution though, i guess not xD):

<script type="text/javascript">
    $.post( 'callDatabase.php', { 'entry[]': ["init"]} );   
</script>

callDatabase.php:

<?php
    header('Content-Type: text/json');

    $test = $_POST['entry'];
    session_start();
    $sID = session_id();

    $timestamp = time();

    $servername = "local";
    $username = "root";
    $password = "rootpw";


    $conn = new mysqli($servername, $username, $password);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }   
    mysqli_select_db($conn, 'myDb');
    $sql = "INSERT INTO myTable (sID, timestamp, t1, rating, start, end, color, fight, completed)
            VALUES ('$sID', '$timestamp', '-', '4', '_', '_', '_', '_', 'false')";

    mysqli_query($conn, $sql);


    mysqli_close($conn); 
    session_unset();
    session_destroy();  
    $_SESSION = array();
?>

Like i said, without the session stuff, it is working fine, with it, the browser is running forever and i get no entry in my database. Since i get no error message i did not find any solution about how to fix it. It is running locally, with XAMPP and a mysql database.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
MasterF
  • 33
  • 1
  • 4
  • *After google'ing a lot that always took me to the same answer (which is not working for me)* what was the answer that did not work for you? Knowing what you've tried helps us show new solutions. – Martin Jan 12 '16 at 20:25
  • Everyone wrote its just as easy as i posted in the question: session_start(); $sID = session_id(); But this does not work – MasterF Jan 12 '16 at 20:30
  • 1
    I was hoping for a link to the attempted solution you'd already tried. So that I/we don't try giving you the same solution again. – Martin Jan 12 '16 at 20:31
  • For example: http://stackoverflow.com/questions/8726268/how-to-get-session-variables-using-session-id http://stackoverflow.com/questions/7068791/how-to-get-set-session-id-or-should-it-be-generated-automatically http://stackoverflow.com/questions/21302733/how-can-i-get-session-id-in-php-and-show-it I think, my main problem is, that i am not sure if it works starting a session in this context or if i have to change a thing, like calling my .php script in a different way or changing sequence of lines of code above – MasterF Jan 12 '16 at 20:33
  • 1
    - What do your PHP error logs say? - What does `echo session_id();` give you? - Try placing the `session_start();` above the `header()` line. – Martin Jan 12 '16 at 20:38
  • This might also be useful to you: http://stackoverflow.com/a/18542272/3536236 – Martin Jan 12 '16 at 20:45
  • The error.log tells me: [Wed Jan 13 10:41:16.333485 2016] [mpm_winnt:notice] [pid 7876:tid 248] AH00428: Parent: child process 5600 exited with status 3221225477 -- Restarting. [Wed Jan 13 10:41:17.079528 2016] [ssl:warn] [pid 7876:tid 248] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name The other messages there seem to be normal, starting worker threads, Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.0 configured and Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/XAMPP/apache' – MasterF Jan 13 '16 at 09:46
  • Bringing the session_start(); to the beginning of the file does not change anything and i get an error while using echo session_id() since no session is started at all. I also tried the two links above which did not work too. They only check if i have a session open, and if not open one. That might not tackle the problem that i cant open one i guess. – MasterF Jan 13 '16 at 09:49
  • Ah and to the error log: I have no skype running, turned of avast and have xampp running as admin – MasterF Jan 13 '16 at 10:00
  • If you can not echo a session_id() then your session is almost certainly not running, so next step is to call up `phpinfo()` and check all the session settings are valid and working, such as you have a correct folder for storing session data server side, and correct permissions etc. – Martin Jan 13 '16 at 10:18
  • If `echo session_id()` causes an error you should also be getting an error from the session_start() command? – Martin Jan 13 '16 at 10:20
  • Also try and just comment out your `header()` line too. Looking more closely, does the `echo session_id()` appear correctly when the page `callDatabase.php` is called directly via the browser (not via ajax) – Martin Jan 13 '16 at 10:31
  • I could fix the problem. Thank you for your help. The code was correct, the problem was caused by xampp. Some ports where blocked since i was running openVPN to get a connection to your testserver. And it seems like xampp has not only problems with blocked ports while running skype, it might also cause problems running other tools that have connection to the Internet/another network. I just build a virtual machine with linux, ran it there and it worked fine! – MasterF Jan 13 '16 at 14:00
  • Good you found the answer. Perhaps useful to others to post and accept an answer your own question with the details from the comment. – Martin Jan 13 '16 at 14:32

1 Answers1

0

Problem is caused by Xampp, since some ports might be blocked, code is working with virtual machine, linux and apache server together with phpmyadmin for example. Checking the errorlogs will help to find out if the blocked ports are causing the problem.

To solve the problem together with xampp, it is recommended to deactivate all tools that might block the needed ports (skype, connection via vpn, antivirus tool, ...) or change the ports to have no conflicts there.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
MasterF
  • 33
  • 1
  • 4