adam87 Posted March 30, 2009 Share Posted March 30, 2009 Hey I've created a login system which works fine and everything but when I login i would like it to get the users ID and store it within the session if possible. Reason being I will need to use the ID to run certain queries at a later date or store details about the certain ID, below is a copy of my homepage where the login script is: <?php session_start(); include 'browser.php'; include 'dbc.php'; $user_name = mysql_real_escape_string($_POST['uname']); if ($_POST['Submit']=='Login') { $md5pass = md5($_POST['pwd']); $sql = "SELECT id,user_name FROM users WHERE user_name = '$user_name' AND user_pwd = '$pwd' AND user_activated='1'"; $result = mysql_query($sql) or die (mysql_error()); $num = mysql_num_rows($result); if ( $num != 0 ) { $_SESSION['user']= $user_name; list($user_id,$user_name) = mysql_fetch_row($result); if (isset($_GET['ret']) && !empty($_GET['ret'])) { header("Location: $_GET[ret]"); } else { header("Location: index.php"); } //echo "Logged in..."; exit(); } header("Location: index.php?msg=Invalid Login"); //echo "Error:"; exit(); } ?> Hope you understand of what I'm tryna get it to do here. Adam Link to comment https://forums.phpfreaks.com/topic/151768-get-users-id/ Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 Try: <?php session_start(); include 'browser.php'; include 'dbc.php'; $user_name = mysql_real_escape_string($_POST['uname']); if ($_POST['Submit']=='Login') { $md5pass = md5($_POST['pwd']); $sql = "SELECT id,user_name FROM users WHERE user_name = '$user_name' AND user_pwd = '$pwd' AND user_activated='1'"; $result = mysql_query($sql) or die (mysql_error()); $num = mysql_num_rows($result); $assoc = mysql_fetch_assoc($result); if ( $num != 0 ) { $_SESSION['user']= $user_name; $_SESSION['uid'] = $assoc['id']; list($user_id,$user_name) = mysql_fetch_row($result); if (isset($_GET['ret']) && !empty($_GET['ret'])) { header("Location: $_GET[ret]"); } else { header("Location: index.php"); } //echo "Logged in..."; exit(); } header("Location: index.php?msg=Invalid Login"); //echo "Error:"; exit(); } ?> Link to comment https://forums.phpfreaks.com/topic/151768-get-users-id/#findComment-796931 Share on other sites More sharing options...
adam87 Posted March 30, 2009 Author Share Posted March 30, 2009 Awesome mate works a treat! Link to comment https://forums.phpfreaks.com/topic/151768-get-users-id/#findComment-796951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.