Jamesking56 Posted January 7, 2008 Share Posted January 7, 2008 my PHP logins do not workI found out I have to use Sessions...but I'm confused...How do I use them? Link to comment Share on other sites More sharing options...
jhecht Posted January 7, 2008 Share Posted January 7, 2008 theres a few ways. The beginners way to use them use with the $_SESSION variable. A lot of beginners(myself included at the time) dump all of the login data into the $_SESSION array. I'll tell you now: BAD IDEA.I'll make a quick mock up of a login page, and then show you what i mean: //Login Pageif(sizeof($_POST) <0){ die("Post data not entered");}else{ session_start(); $user = $_POST['user']; $pw = md5($_POST['pass']); //User and Pw variables Passwords has been md5 encrypted $sql = "SELECT user_id FROM user_table WHERE user_name = '$user' AND password = '$pw'"; $ans = mysql_query($sql,$yourDbConnection) or die(mysql_error()); if(mysql_num_rows($ans)==1){ $row = mysql_fetch_assoc($ans); $_SESSION['user_id']= $row['user_id']; }} Now, on all pages that require the user to be logged in, we do this: session_start(); if(!isset($_SESSION['user_id'])){ die('you are not logged in'); } $sql = "SELECT * FROM user_table WHERE user_id = ".$_SESSION['user_id']; $ans = mysql_query($sql,$yourDbConnection) or die(mysql_error()); $user_data = mysql_fetch_assoc($ans);// all user data is now in the user_data array. Every column in your db is now a key for the array here. echo "Hello <b>".$user_data['user_name']."<b>"; if you need the $_SESSION data, you MUST call session_start before anything else happens. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.