Closed Thread
Results 1 to 10 of 10

Thread: Saving session into database and use it

  1. #1
    yonghan is offline Learning Programmer
    Join Date
    May 2008
    Posts
    99
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

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

  4. #3
    yonghan is offline Learning Programmer
    Join Date
    May 2008
    Posts
    99
    Rep Power
    0

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

  5. #4
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    Re: Saving session into database and use it

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

  6. #5
    yonghan is offline Learning Programmer
    Join Date
    May 2008
    Posts
    99
    Rep Power
    0

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

  7. #6
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    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.

  8. #7
    yonghan is offline Learning Programmer
    Join Date
    May 2008
    Posts
    99
    Rep Power
    0

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

  9. #8
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    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.

  10. #9
    yonghan is offline Learning Programmer
    Join Date
    May 2008
    Posts
    99
    Rep Power
    0

    Re: Saving session into database and use it

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

  11. #10
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    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
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. saving data in a database
    By ployo60 in forum Pascal and Delphi
    Replies: 7
    Last Post: 02-20-2011, 11:44 AM
  2. How to save session or cookie in database?
    By Hamed in forum PHP Development
    Replies: 10
    Last Post: 12-06-2010, 06:43 AM
  3. php shopping cart, database or session?
    By Jacki in forum PHP Development
    Replies: 5
    Last Post: 10-18-2009, 02:10 PM
  4. Saving a excel file to Oracle Database using vb6.0
    By catchsyed in forum Visual Basic Programming
    Replies: 4
    Last Post: 05-04-2009, 03:07 PM
  5. saving to database after few seconds
    By iory in forum PHP Development
    Replies: 4
    Last Post: 10-23-2008, 07:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

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