wdlyons Posted April 2, 2012 Share Posted April 2, 2012 Hi, I have created a session based logon system using php and MYSQL from some tutorials I found online which is working very successfully. I can log on and of and move through different pages with no problems. My query is how do I output or display the information that is specific to the user which is currently logged on and block access to any other users information. I am quite sure there is a simple solution that is escaping me. If you could point me in the right direction it would be greatly appreciated. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/260172-accessing-user-specific-information-from-a-mysql-database/ Share on other sites More sharing options...
creata.physics Posted April 2, 2012 Share Posted April 2, 2012 What exactly do you mean by infromation specific to the user? Depending on how you track a users online status you can query your users table asking for that users information: $query = mysql_query("select username from tables where id = '{$_COOKIE['user_id']}'"); $result = mysql_fetch_assoc( $query ); echo 'Hello ' . $result['username']; Of course if you don't use cookies you'll need to either check $_SESSION or your mysql table that tracks user sessions. That part depends on how your code is set up. Quote Link to comment https://forums.phpfreaks.com/topic/260172-accessing-user-specific-information-from-a-mysql-database/#findComment-1333464 Share on other sites More sharing options...
wdlyons Posted April 2, 2012 Author Share Posted April 2, 2012 Hi, Sorry I should have put the code: First File: auth.php <?php //Start session session_start(); //Check whether the session variable SESS_MEMBER_ID is present or not if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) { header("location: access-denied.php"); exit(); } ?> Second File: Profile.php <?php require_once('auth.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>My Profile</title> <link href="loginmodule.css" rel="stylesheet" type="text/css" /> </head> <body> <h1>My Profile </h1> <a href="member-index.php">Home</a> | <a href="logout.php">Logout</a> <?php //Include database connection details require_once('config.php'); //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } // Collects data from "members" table $data = mysql_query("SELECT * FROM members") or die(mysql_error()); // puts the "friends" info into the $info array $info = mysql_fetch_array( $data ); // Print out the contents of the entry Print "<b>Member ID:</b> ".$info['member_id'] . " "; Print "<b>First Name:</b> ".$info['firstname'] . " "; Print "<b>Last Name:</b> ".$info['lastname'] . " <br><br>"; Print "</table>"; ?> </body> </html> I will try and explain a little better too!! When a user logs in and selects the link for their profile they only see the first row in the table not their own details. I am seeking guidance on how I would go about showing the logged on user (who has the session started) details only ie by somehow using the users member_id or login id as a way to fetch the information out of the table. I hope this makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/260172-accessing-user-specific-information-from-a-mysql-database/#findComment-1333471 Share on other sites More sharing options...
wdlyons Posted April 2, 2012 Author Share Posted April 2, 2012 Hi Thanks for your help, we seemed to have fixed the issue with the line $data = mysql_query("SELECT * FROM members WHERE member_id='{$_SESSION['SESS_MEMBER_ID']}'") or die(mysql_error()); Thanks Again Quote Link to comment https://forums.phpfreaks.com/topic/260172-accessing-user-specific-information-from-a-mysql-database/#findComment-1333479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.