James Dale
James Dale

Reputation: 126

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?

--------------------------------------------UPDATE---------------------------------------

HTML FORM

<div class="login">
<form id="login" method="post" action="includes/checklogin.php">
<p>Login</p>
<p>Username</p>
<input name="user_name" id="user_name">
<p>Password</p>
<input name="password" id="password" type="password">
</br>
<input type="submit" name="Submit" value="Login">
</form>
</div>
<!--<img src="images/logo.png" alt="The Community">-->
</div>

checklogin.php

<?php

$con = mysql_connect("****","***","***");
            if (!$con)
              {
              die('Could not connect: ' . mysql_error());
              }
       //Finds database
            mysql_select_db("****", $con);

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

$user_name = stripslashes($user_name);
$password = stripslashes($password);
$user_name = mysql_real_escape_string($user_name);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM fyp_users WHERE user_name='{$user_name}' and       password='{$password}'";
$result=mysql_query($sql);



$count=mysql_num_rows($result);

if($count==1) {

session_register("user_name");
session_register("password"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

login_success,php

<?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'];
$password = $_SESSION['password'];
$_SESSION['views']=200;
header('location:../profile.php');


?>

profile.php

<?php
session_start();


echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']


?>

Thanks James

Upvotes: 0

Views: 9265

Answers (3)

Rahul
Rahul

Reputation: 1181

<?php
session_start(); //use this to invoke session variable usage

echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']


?>



if(!session_is_registered(user_name)); // this statement seems incorrect

it should be rather

<?php

session_start();
if(!session_is_registered(user_name)) : // it should be colon with endif at end
// session_is_registered('user_name')  user_name should be within single quotes untill its DEFINED

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

endif; // here is end if

?>

As i think you are using upgraded version of php so there could be one more Reason as:

session_is_registered(user_name)

This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

source: http://php.net/manual/en/function.session-is-registered.php

Upvotes: 0

Tushar Gupta
Tushar Gupta

Reputation: 15923

you have start the session on the next page also use

session_start(); 

echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']

Upvotes: 1

Sasanka Panguluri
Sasanka Panguluri

Reputation: 3128

In your code, include a Session_start at the very first line. Session_start is a must if your code uses sessions. What it does is, resume an existing session if at all it exists, or start a new fresh session if there is no session already existing.

<?php
session_start();


echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name'];


?>

Upvotes: 3

Related Questions