Jump to content

unable to show current logged in user


yobo

Recommended Posts

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

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);

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.

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

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.

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..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.