+ Reply to Thread
Results 1 to 10 of 10

Thread: Saving session into database and use it

  1. #1
    Learning Programmer yonghan is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    96

    Saving session into database and use it

    Hi all,i need help regarding saving session into mysql and use it..Right now i'm using this code...

    auth.php

    Code:
    session_start();
    include(
    'db.inc.php');
    $email=mysql_real_escape_string($_POST['email']);
    $pwd=mysql_real_escape_string($_POST['pwd']);
    $sql="Select member_id,email,password,nama,type from users where email='$email' and password=md5('$pwd')";
    $exec=mysql_query($sql);
    $result=mysql_fetch_array($exec);
    if (
    $result['type'] == "member")
    {
    $_SESSION['nama']=$result['nama'];
    $_SESSION['id']=$result['member_id'];
    header('location:member.php');
    }
    else
    {
        echo 
    'Anda gagal login';
       
    header('location:index.php');

    member.php

    Code:
    session_start();
    include(
    'output_fns.php');
    if(!
    $_SESSION['nama'])
    {
        
    header('location:index.php');
    }
    else
    {
    do_kepala('Member');
    echo 
    'Welcome    ' $_SESSION['nama'];
    menu_member(); 
    What should i do to save the session into mysql..I don't have a clue...Thanks a lot...

  2. #2
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,619
    Blog Entries
    7

    Re: Saving session into database and use it

    This is what I do on each page:

    Code:
    $sessname 'MySystem';
    $sesslength 3600 // one hour
    session_name($sessname);
    session_start();

    // Reset the expiration time upon page load
    if (isset($_COOKIE[$sessname])) {
        
    setcookie($sessname$_COOKIE[$sessname], time() + $sesslength"/");

    to enter the session id to the database, just use the function session_id() which returns the id to be entered to the database and compared to the last value...

  3. #3
    Learning Programmer yonghan is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    96

    Re: Saving session into database and use it

    Hi..have you ever done using database to hold sessions and call it?I'm intending to look for that tutorial..Thanks...

  4. #4
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,619
    Blog Entries
    7

    Re: Saving session into database and use it

    you mean to hold the session data or only the session id?

  5. #5
    Learning Programmer yonghan is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    96

    Re: Saving session into database and use it

    Holding

    Code:
    $_SESSION['nama']=$result['nama'];
    $_SESSION['id']=$result['member_id']; 
    Is it possible to do that?Thanks orjan..

  6. #6
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,619
    Blog Entries
    7

    Re: Saving session into database and use it

    Well, the session variables are stored on the server and can be retrieved on the next page by just referring to the session id from the user. but if you want to use the database as a sesstion data storage, you need to elaborate with session_set_save_handler and create functions on how the session will handle data.

    EDIT: but of course it's possible to store sql data into the session to check it's legitimy. actually it's almost needed.

  7. #7
    Learning Programmer yonghan is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    96

    Re: Saving session into database and use it

    Have you ever done it before?Can you please give me examples how to do it??Thanks a lot...

  8. #8
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,619
    Blog Entries
    7

    Re: Saving session into database and use it

    storing the id for legitimate checks, or letting the session module use mysql as storage?

    I almost always use the session id for legitimate checks.
    it's usually very simple, just add a "session" field to the user-table and store the session-id there, then store the user-id in the session. if it matches on next page both ways, the session is valid.

  9. #9
    Learning Programmer yonghan is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    96

    Re: Saving session into database and use it

    Hi Jan,how to let the session module use mysql as storage..

  10. #10
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,619
    Blog Entries
    7

    Re: Saving session into database and use it

    No I haven't used it, but there are examples for it at PHP: session_set_save_handler - Manual
    __________________________________________
    Ask me: Orjan | Contribute to the Wiki! | Make your own Programming blog!
    I usually play eRepublik and Travian when I'm not on CodeCall

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts