Wednesday, 21 August 2013

Get username from the database from current session

Get username from the database from current session

So been looking on the web for some time and have attempted to follow
examples...
What I want to do is get the user to login and then a session to start
which I have and have tested and it does work, however I'm having trouble
getting the username or any other item of data from the database.
The user logs in with the username and password. They are then directed to
another page with this code...
<?php
session_start();
if(!session_is_registered(user_name));
$con = mysql_connect("****","****","****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Finds database
mysql_select_db("****", $con);
$result = mysql_query("SELECT * FROM fyp_users;");
$first_name = $_SESSION['first_name'];
$_SESSION['views']=200;
header('location:../profile.php');
?>
The Session views thing was just a test and that works find and displays
the number when the user is logged in. I have then attempted to do the
first_name session.
This is the page that it then redirects too...
<?php
$con = mysql_connect("****","****","****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Finds database
mysql_select_db("****", $con);
?>
<?php
session_start( );
//$result = mysql_query("SELECT * FROM fyp_users;");
?>
Some HTML....
<?php
echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['$first_name']
?>
How it's not displaying the name, I know no if it's an easy fix or not,
can anybody help me?
Thanks James

No comments:

Post a Comment