Cannot get user info to show up

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Cannot get user info to show up

Post by khushbush »

When a user logs into my application, I want them to be able to view their details. Unfortunately, that doesn't seem to happen with my application... :S

Here's the code...I can't seem to find what's wrong with it.

Code: Select all

 
<?
/**
 * UserInfo.php
 *
 * This page is for users to view their account information
 * with a link added for them to edit the information.
 *
 */
include("session.php");
?>
 
<html>
<title>Capital Abode</title>
<body>
 
<?
/* Requested Username error checking */
$req_user = trim($_GET['username']);
if(!$req_user || strlen($req_user) == 0 ||
   !eregi("^([0-9a-z])+$", $req_user) ||
   !$database->usernameTaken($req_user)){
   die("Username not registered");
}
 
/* Logged in user viewing own account */
if(strcmp($session->username,$req_user) == 0){
   echo "<h1>My Account</h1>";
}
/* Visitor not viewing own account */
else{
   echo "<h1>User Info</h1>";
}
 
/* Display requested user information */
$req_user_info = $database->getUserInfo($req_user);
 
/* Username */
echo "<b>Username: ".$req_user_info['username']."</b><br>";
 
/* Email */
echo "<b>Email:</b> ".$req_user_info['userEmail']."<br>";
 
/**
 * Note: when you add your own fields to the users table
 * to hold more information, like homepage, location, etc.
 * they can be easily accessed by the user info array.
 *
 * $session->user_info['location']; (for logged in users)
 *
 * ..and for this page,
 *
 * $req_user_info['location']; (for any user)
 */
 
/* If logged in user viewing own account, give link to edit */
if(strcmp($session->username,$req_user) == 0){
   echo "<br><a href=\"useredit.php\">Edit Account Information</a><br>";
}
 
/* Link back to main */
echo "<br>Back To [<a href=\"main.php\">Main</a>]<br>";
 
?>
 
</body>
</html>
 
 
This is code from session.php:

Code: Select all

 
 
   /**
    * checkLogin - Checks if the user has already previously
    * logged in, and a session with the user has already been
    * established. Also checks to see if user has been remembered.
    * If so, the database is queried to make sure of the user's 
    * authenticity. Returns true if the user has logged in.
    */
   function checkLogin(){
      global $database;  //The database connection
      /* Check if user has been remembered */
      if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
         $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
         $this->userid   = $_SESSION['userid']   = $_COOKIE['cookid'];
      }
 
      /* Username and userid have been set and not guest */
      if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
         $_SESSION['username'] != GUEST_NAME){
         /* Confirm that username and userid are valid */
         if($database->confirmUserPass($_SESSION['username'], $_SESSION['userid']) != 0){
            /* Variables are incorrect, user not logged in */
            unset($_SESSION['username']);
            unset($_SESSION['userid']);
            return false;
         }
 
         /* User is logged in, set class variables */
         $this->userinfo  = $database->getUserInfo($_SESSION['username']);
         $this->username  = $this->userinfo['username'];
         $this->userid    = $this->userinfo['userid'];
         $this->userlevel = $this->userinfo['userLevel'];
         return true;
      }
      /* User not logged in */
      else{
         return false;
      }
   }
 
And this is from database.php:

Code: Select all

 
    /**
    * usernameTaken - Returns true if the username has
    * been taken by another user, false otherwise.
    */
   function usernameTaken($username){
         if(!get_magic_quotes_gpc()){
         $username = addslashes($_POST['username']);
      }
      $q = "SELECT username FROM ".TBL_USERS." WHERE username = '$username'";
      $result = mysql_query($q, $this->connection);
      if(!$result || (mysql_num_rows($result) < 1)){
         return 1; //Indicates username failure
   }
   }
 
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Erm...hello? Any help...? Please?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Cannot get user info to show up

Post by John Cartwright »

Per our forum rules, we only allow a bump after a minimum of 24 hours.

As for your question, what exactly do you mean they cannot view their user details? What kind of details? Please be far more specific with your questions(s).
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Oh sorry. My apologies. :oops:

Details such as username and email address at the least. I can then add any other details once I get these two to show up. At the VERY least, it would be nice to have the username show up, but unfortunately, it doesn't seem to do even that. I think it's something to do with the code in database.php, but I can't pinpoint the problem :?

Thank you for asking me to clarify my problem...I'll revise the forum rules too. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Cannot get user info to show up

Post by John Cartwright »

It's a bit difficult to debug somebody elses code like this through a forum ;)

My suggested would be to add as many debugging lines as possibly to determine where exactly the desired effect is not being acheived (I've highlighted some examples).

i.e.

Code: Select all

 
$sql = 'SELECT * FROM users';
$result = mysql_query($sql)[color=#FF0000] or die(mysql_error());[/color]
 
while ($row = mysql_fetch_assoc($result)) {
   $users[] = $row;
}
 
[color=#FF0000]echo '<pre>'. print_r($users, true) .'</pre>';[/color]
 
 
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Ok...so having decided on a new and much simpler approach, I am still trying to get the user's username to show up...but this time upon login instead of when accessing the user's details.

Now...my current issue is that the username is just not showing up on the page. I only get the menu and the sign "Logged In" to show up.

Here is the troublesome code fragment...where am I going wrong?

Code: Select all

 
<?
 
mysql_connect("localhost", "root", "khushbakht") or die(mysql_error());
 
mysql_select_db("user") or die(mysql_error());
 
   echo "<h1>Logged In</h1>";
 
if (isset ($_GET['username'])){
$user = mysql_real_escape_string($main->$_POST['username']);
$query = "SELECT username FROM user WHERE username = $user";
$result = mysql_query($query);
 
 
   
   while ($row = mysql_fetch_row($result)) {
   
   echo "Welcome <b>$row[username]</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$row[username]\">My Details</a>] &nbsp;&nbsp;"
       ."[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;";
       }
       }
       die(mysql_error());
       
       if($session->isAdmin()){
      echo "[<a href=\"admin.php\">Admin Center</a>] &nbsp;&nbsp;";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
 
else {
  header("Location: main.php");
}
 
?>
 
Thank you in advance to anyone who can help me! :)
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Ok...so having decided on a new and much simpler approach, I am still trying to get the user's username to show up...but this time upon login instead of when accessing the user's details.

Now...my current issue is that the username is just not showing up on the page. I only get the menu and the sign "Logged In" to show up.

Here is the troublesome code fragment...where am I going wrong?

Code: Select all

 
<?
 
mysql_connect("*************", "******", "********") or die(mysql_error());
 
mysql_select_db("user") or die(mysql_error());
 
   echo "<h1>Logged In</h1>";
 
if (isset ($_GET['username'])){
$user = mysql_real_escape_string($main->$_POST['username']);
$query = "SELECT username FROM user WHERE username = $user";
$result = mysql_query($query);
 
 
   
   while ($row = mysql_fetch_row($result)) {
   
   echo "Welcome <b>$row[username]</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$row[username]\">My Details</a>] &nbsp;&nbsp;"
       ."[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;";
       }
       }
       die(mysql_error());
       
       if($session->isAdmin()){
      echo "[<a href=\"admin.php\">Admin Center</a>] &nbsp;&nbsp;";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
 
else {
  header("Location: main.php");
}
 
?>
 
Thank you in advance to anyone who can help me! :)
User avatar
bovermyer
Forum Commoner
Posts: 25
Joined: Tue Apr 08, 2008 9:14 am
Location: South Dakota

Re: Cannot get user info to show up

Post by bovermyer »

First, on this line -

echo "Welcome <b>$row[username]</b>, you are logged in. <br><br>"

...as well as the following line, try switching $row['username'] for $row[username]. I'm not sure if that'll work, but it's worth a shot.

-

Second, I strongly suggest using a user database schema that includes a userid as a primary key. Right now, this line:

$query = "SELECT username FROM user WHERE username = $user";


...seems redundant, and only adds additional server hits.

-

Third, on line 10 you have a $_GET['username'], but on the next line you have $_POST['username']. Which are you using?
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Hi, thanks for the swift reply.

I want to use GET. I'm trying to use POST as this is the username entered on the login page, but obviously that doesn't seem to work. I'm quite confused as to which is the best method to use...I guess whichever one works!

I'll try to change the username to userid in my database and see if that works.

Thanks. :)
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Ok...made the changes...still no luck.

Code as of now:

Code: Select all

 
if (isset ($_GET['userid'])){
$user = mysql_real_escape_string($_GET['userid']);
$query = "SELECT username FROM user WHERE userid = $user";
$result = mysql_query($query);
 
 
   
   while ($row = mysql_fetch_row($result)) {
   
   echo "Welcome <b>$row[username]</b>, you are logged in. <br><br>"
       ."[<a href=\"userinfo.php?user=$row[username]\">My Details</a>] &nbsp;&nbsp;"
       ."[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;";
       }
       }
       die(mysql_error());
       
       if($session->isAdmin()){
      echo "[<a href=\"admin.php\">Admin Center</a>] &nbsp;&nbsp;";
   }
   echo "[<a href=\"process.php\">Logout</a>]";
}
 
else {
  header("Location: main.php");
}
 
Message to moderators: I apologise for not adding html and php tags. I have tried to follow the instructions, but I can't seem to get the tags to work. Maybe I'm just not doing it right :?
User avatar
bovermyer
Forum Commoner
Posts: 25
Joined: Tue Apr 08, 2008 9:14 am
Location: South Dakota

Re: Cannot get user info to show up

Post by bovermyer »

Change this line:

echo "Welcome <b>$row[username]</b>, you are logged in. <br><br>"

to read:

echo "Welcome <b>$row['username']</b>, you are logged in. <br><br>"
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

I got this error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Documents and Settings\Desktop\Project\loggedin.php on line 31
User avatar
bovermyer
Forum Commoner
Posts: 25
Joined: Tue Apr 08, 2008 9:14 am
Location: South Dakota

Re: Cannot get user info to show up

Post by bovermyer »

Okay, then revert that last change.

Have a look at this:

How to create a login form in PHP
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot get user info to show up

Post by khushbush »

Erm...I'll keep looking...but still no luck :(

Thanks, bovermyer.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Cannot get user info to show up

Post by John Cartwright »

Code: Select all

$user = mysql_real_escape_string($_GET['userid']);
$query = "SELECT username FROM user WHERE userid = $user";
You understand that all they need to do here is supply a correct username to be logged in, right?
Post Reply