0

After a successful login attempt my php script starts a session like this:

    session_set_cookie_params(1800,'/','www.mydomain.com',true);
    session_start();
    header("location:mainpage.html");

Now my questions are:

  • How can I save my user-id to my session for further use?
  • Would it be sufficient to add these 3 code snippet to keep my sessions alive in a jQuery Mobile page:

mysession.php:

     <?php
     session_set_cookie_params(1800,'/','www.mydomain.com',true);
     session_start();
     ?>

somewhere in mainpage.html:

    function getHttpRequestObj()
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            return new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    function callSession(id)
    {
    var xmlhttp = getHttpRequestObj();
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById(id).innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","mysession.php",true);
    xmlhttp.send();
    }

And then in the outmost div (name='main') in mainpage.html

      $(document).ready(function() {
callSession('main');
});
5
  • 5
    session_register This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0. Commented Jul 9, 2012 at 7:14
  • That helps. Then I would need a new method of somehow storing my user-id in my session for further use and validation. So basically I am now only using the session_set_cookie_params and session_start part with only the direct header to mainpage.html as long as the MySQL query is authenticated and login information is correct. Commented Jul 9, 2012 at 7:23
  • 2
    you only need session_start() than then just use the $_SESSION global array Commented Jul 9, 2012 at 7:24
  • So a UID is automatically created for me? I still need to use the username to get the userid from MySQL though, unless I've overseen something. Commented Jul 9, 2012 at 7:41
  • session_set_cookie_params(1800,'/','www.mydomain.com',true); session_start(); $_SESSION['id'] == $userid; header("location:mainpage.html"); - A tad hard to read but I think that'll do as far as saving my userid from MySQL goes. Commented Jul 9, 2012 at 8:07

1 Answer 1

2

Problem solved by adding the following code at the top of my mainpage.html and renaming it to mainpage.php

<?php
session_start();
$id = $_SESSION['id'];
?>

Now I can access my userid from anywhere by doing

<? echo $id ?>
Sign up to request clarification or add additional context in comments.

1 Comment

or you could just do echo $_SESSION['id'] ?

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.