Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Saving session into database and use it

session

  • Please log in to reply
9 replies to this topic

#1 yonghan

yonghan

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 103 posts

Posted 21 February 2009 - 04:12 AM

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

auth.php


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


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

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 21 February 2009 - 06:31 AM

This is what I do on each page:


$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...
  • 0

#3 yonghan

yonghan

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 103 posts

Posted 21 February 2009 - 07:18 AM

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

#4 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 21 February 2009 - 07:30 AM

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

#5 yonghan

yonghan

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 103 posts

Posted 21 February 2009 - 07:38 AM

Holding


$_SESSION['nama']=$result['nama'];
$_SESSION['id']=$result['member_id'];


Is it possible to do that?Thanks orjan..
  • 0

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 21 February 2009 - 08:51 AM

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

#7 yonghan

yonghan

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 103 posts

Posted 21 February 2009 - 11:52 PM

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

#8 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 22 February 2009 - 02:44 AM

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

#9 yonghan

yonghan

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 103 posts

Posted 22 February 2009 - 03:25 AM

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

#10 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 22 February 2009 - 04:13 AM

No I haven't used it, but there are examples for it at PHP: session_set_save_handler - Manual
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Also tagged with one or more of these keywords: session

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download