0

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

James Dale
  • 126
  • 1
  • 4
  • 11
  • 1
    `$_SESSION['first_name']` and not `$_SESSION['$first_name']` and you need to call `session_start();` at the top. – Prix Aug 21 '13 at 23:12
  • James, where is your login HTML and code? Also this is an unneeded line `if(!session_is_registered(user_name));`, On the above code I cant see your user log into your page, there is no MySQL that looks for the user that tried to login and no code that assigns that user to the session. **The only MySQL query you have is one that returns all of your users.** – Prix Aug 21 '13 at 23:23
  • Ahh so is that the problem? To be honest I jsut assumed it would work from that. – James Dale Aug 21 '13 at 23:26
  • It can be the problem, but without seeing the rest of your code I can't say. – Prix Aug 21 '13 at 23:27
  • Login Form ` ` – James Dale Aug 21 '13 at 23:28
  • Updated the original post :) – James Dale Aug 21 '13 at 23:40
  • Well not sure what tutorial you're following but it's terribad see here an example of how to assign a variable to the session http://stackoverflow.com/a/10097986/342740 and a full tutorial http://net.tutsplus.com/tutorials/php/user-membership-with-php/?search_index=47 – Prix Aug 22 '13 at 00:38

3 Answers3

3

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'];


?>
Sasanka Panguluri
  • 3,058
  • 4
  • 32
  • 54
1

you have start the session on the next page also use

session_start(); 

echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0
<?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

Rahul
  • 1,181
  • 1
  • 11
  • 20