Forum Moderators: coopster

Message Too Old, No Replies

Displaying the user logged in

         

m4tt

2:20 pm on Mar 6, 2006 (gmt 0)

10+ Year Member



I have been working for the last 12 hours trying to make the user session display a username after logging in with no success, can anyone help?

I would greatly appreciate any help as I am going in circles!

This file processes:

<?php
// we must never forget to start the session
session_start();

$errorMessage = '';
if (isset($_POST['username']) && isset($_POST['password'])) {
include 'library/config.php';
include 'library/opendb.php';

$username = $_POST['username'];
$password = $_POST['password'];

// check if the user id and password combination exist in database
$sql = "SELECT user_id
FROM users
WHERE user_id = '$username' AND user_password = PASSWORD('$password')";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

if(mysql_num_rows($result)) {
$_SESSION['loggedin'] = 1;
$_SESSION['username'] = '$username';
$_SESSION['password'] = '$password';

}
// move user

$sql = "SELECT url
FROM users
WHERE user_id = '$username' AND user_password = PASSWORD('$password')";

$result = mysql_query("SELECT url FROM users
WHERE user_id = '$username' AND user_password = PASSWORD('$password') ");
$row = mysql_fetch_row($result) or die ( header("Location: error.php"));
$url = $row['0']; // define the correct element for the field url

// after login we move to the main page
// or to admin
// or to failsafe
header("Location: index.php");

exit;
}
?>
<? include ('includes/config.php');?>
<? include ('includes/header_login.php');?>

<!-- Start Page -->

<div id="maincol">
<br />

<fieldset><legend>Site Manage - Login</legend>
<div id="cform">
<form action="login.php" method="post" name="frmLogin" id="frmLogin">
<div class="top">
<span class="lab">Username:</span>
<span class="layer">
<input name="username" type="text" id="username"><br />
</span>
</div>

<div class="top">
<span class="lab">Password:</span>
<span class="layer">
<input name="password" type="password" id="password"><br />
</span>
</div>
<div class="top">
<span class="layer">
<input name="btnLogin" type="submit" id="btnLogin" value="Login">
</span>
</div>

</form>
</div>
</fieldset>

This file is the include file on each page:

<?php
session_start();

// is the one accessing this page logged in or not?
if (!isset($_SESSION['loggedin']))

{
// not logged in, move to login page
header('Location: /login.php');

}
include("../library/config.php");
include("../library/opendb.php");

$id=$_POST['id'];
$query=" SELECT * FROM users";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();

$i=0;
while ($i < $num) {
$user_id=mysql_result($result,$i,"user_id");
?>

<?
++$i;
}
?>

Then I try to display the user with:

<? SESSION_$('username')?>

Scally_Ally

2:28 pm on Mar 6, 2006 (gmt 0)

10+ Year Member



could you be a little more specific?

Is it that there is nothing stored in the session variable $_SESSION('username').. or are you allowed to even log in at all?

this is prob just a type error but you are not using echo to write to the page at the bottom <?=$_SESSION('username')?>

Ally

dreamcatcher

4:44 pm on Mar 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, no echo. Use the following to test your session array to see if its populating with the correct data;

print_r($_SESSION);

dc

m4tt

10:33 pm on Mar 6, 2006 (gmt 0)

10+ Year Member



Sorry, to be more specific I would like to show which user is logged in after login. It does login but it doesn't show the user, so it is probably not storing.

Where would I add the print_r($_SESSION);?

m4tt

12:17 am on Mar 7, 2006 (gmt 0)

10+ Year Member



After doing the session print I get this:

Array ( [loggedin] => 1 [username] => $username [password] => $password )

so it looks like it is not adding the user to the session?

Should it be showing admin ie. if the admin was logged in?

coopster

12:24 am on Mar 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep, that would be the problem. Do you understand what is happening? You are assiging the string as opposed to the value. Your problem lies here:
if(mysql_num_rows($result)) { 
$_SESSION['loggedin'] = 1;
$_SESSION['username'] = '$username';
$_SESSION['password'] = '$password';
}

See PHP Strings [php.net] to understand what is happening.

m4tt

12:31 am on Mar 7, 2006 (gmt 0)

10+ Year Member



I sort of understand so would I need to change the quotes?

Excuse my bad knowledge..

m4tt

12:38 am on Mar 7, 2006 (gmt 0)

10+ Year Member



Thanks

So how would I then output the username?

Would I use the print_r function?

m4tt

12:43 am on Mar 7, 2006 (gmt 0)

10+ Year Member



Sorry I get this error message:

Fatal error: Call to undefined function: array() in /home/symmetry/public_html/sitemanage/index.php on line 16

when adding the <?=$_SESSION('username')?>

coopster

12:44 am on Mar 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Right, you could either change the quotes or at this point you don't even need the quotes since the variable itself is the string.
if(mysql_num_rows($result)) {  
$_SESSION['loggedin'] = 1;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}

As far as print_r, no, what was being shown to you there is a method of finding out what values an array contains. It is very handy for troubleshooting. To access any given index in the $_SESSION array you can use print_r to dump the array out, then you will see what index applies to each value. You have done this, that is why you seen:
Array ( 
[loggedin] => 1
[username] => $username
[password] => $password
)

So, now to print out the username you can follow the instructions earlier, before you had your string fixed.
print $_SESSION['username'];

coopster

12:45 am on Mar 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You posted while I was replying. Don't forget now, $_SESSION variables are NOT available until you start_session() first!

m4tt

12:46 am on Mar 7, 2006 (gmt 0)

10+ Year Member



Thanks heaps coopster!