yobo Posted March 11, 2007 Share Posted March 11, 2007 i am not sure what is going on but when i login and that works fine but if i want to show the the user who they are logged in as in the members section for example you are loged in as who ever, it does not show. here is my code for the members section <?php session_start(); if (!isset($_COOKIE['loggedin'])) die("You are not logged in!"); include("config.php"); // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); $showuser = "SELECT * FROM users"; $user = mysql_query($showuser) or die ("Could not show user because ".mysql_error()); $row=mysql_fetch_assoc($user); $userid = $row['userid']; $username = $row['username']; echo "you are logged in as $username"; ?> <a href="logout.php">Logout Here</a> Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/ Share on other sites More sharing options...
chrisuk Posted March 11, 2007 Share Posted March 11, 2007 where you have: $row=mysql_fetch_assoc($user); you need to use the extract function before you assign the username/userid variables so the whole thing is: <?php $row=mysql_fetch_assoc($user); extract ($row); ?> if you have problems after try replacing $row=mysql_fetch_assoc($user); with $row = mysql_fetch_array($user, MYSQL_ASSOC); Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204779 Share on other sites More sharing options...
trq Posted March 11, 2007 Share Posted March 11, 2007 Your query... $showuser = "SELECT * FROM users"; Will select all users in your database. Is this really what you want? Ideally you should store your username in the $_SESSION array when you actually log in. The code you have posted is probelmatic and simply not needed. Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204782 Share on other sites More sharing options...
tauchai83 Posted March 11, 2007 Share Posted March 11, 2007 $query="select * FROM user WHERE userid='$userid' "; //to retrive particular user record. to be more helpful..haha $userid=$_POST['userid']; $_SESSION['userid']=$userid; creating session variable is easier. Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204783 Share on other sites More sharing options...
yobo Posted March 11, 2007 Author Share Posted March 11, 2007 ok i am still having problems here is my login code <?php include("config.php"); // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); $match = "SELECT * FROM users WHERE username = '".$_POST['username']."' and password = '".$_POST['password']."';"; $qry = mysql_query($match) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows <= 0) { echo "Sorry, there is no username $username with the specified password.<br>"; echo "<a href=login.php>Try again</a>"; exit; } else { $row=mysql_fetch_assoc($qry); $status=$row['activated']; $id = $row['id']; $userid = $row['userid']; $username = $row['username']; if ($status=='yes'){ setcookie("loggedin", "TRUE", time()+(3600 * 24)); setcookie("username", "$username"); echo "You are now logged in! "; echo "Continue to the <a href=members.php?userid=$userid>members section."; } else{ echo "Your account is inactive. Please activate your account before proceed"; } } ?> and my members code <?php session_start(); if (!isset($_COOKIE['loggedin'])) die("You are not logged in!"); include("config.php"); // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); $username=$_GET['username']; $_SESSION['username']=$username; echo "you are logged in as $username"; ?> <a href="logout.php">Logout Here</a> p.s. i am not advanced in php i am only a newbie so i am learning Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204824 Share on other sites More sharing options...
trq Posted March 11, 2007 Share Posted March 11, 2007 Still having problems means nothing to us Im sorry. You need to tell us what the problems are. Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204829 Share on other sites More sharing options...
yobo Posted March 11, 2007 Author Share Posted March 11, 2007 sure i am still unable to show the currrent logged in user Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204836 Share on other sites More sharing options...
Orio Posted March 11, 2007 Share Posted March 11, 2007 Are you getting an error? Or are you simply getting the message "You are now logged in as "? Maybe you are getting a blank page? Share some info! One thing I've spotted in the login page: This- echo "Continue to the <a href=members.php?userid=$userid>members section."; Should be- echo "Continue to the <a href=members.php?userid=$username>members section."; Orio. Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204839 Share on other sites More sharing options...
yobo Posted March 11, 2007 Author Share Posted March 11, 2007 thank you orio for that i have now got it working also i just wanted to know if i should replace cookies with sessions in my login script for security reassons? my login script will only be used for users to submit hacks to my site and to have there own profiles etc.. Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204845 Share on other sites More sharing options...
Orio Posted March 11, 2007 Share Posted March 11, 2007 I personally prefer sessions. I think they are easier to handle and are easier to secure. But it doesn't really matter- each method has it's good sides and it's bad sides. Orio. Link to comment https://forums.phpfreaks.com/topic/42218-unable-to-show-current-logged-in-user/#findComment-204847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.