0

So its the final step of a college project... i'm trying to get a value from a mysql table dependant from the session. For example, you enter your email and password, the next page you are redirected two has your first name. I'm using this script for the login check:

<?php
   ob_start();
   $host="localhost";
   $username="***"; 
   $password="***";  
   $db_name="***"; 
   $tbl_name="usersWebsite";

   mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
   mysql_select_db("$db_name")or die("cannot select DB");

   $myusername=$_POST['myusername']; 
   $mypassword=$_POST['mypassword']; 

   $myusername = stripslashes($myusername);
   $mypassword = stripslashes($mypassword);
   $myusername = mysql_real_escape_string($myusername);
   $mypassword = mysql_real_escape_string($mypassword);
   $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
   $result=mysql_query($sql);

   $count=mysql_num_rows($result);

   if($count==1){
      session_register("myusername");
      session_register("mypassword"); 
      header("location:login_success.php");
   }
   else {
      echo "Wrong Username or Password";
   }
   ob_end_flush();
?>

and i'm using this short code to create the session:

<?php
    session_start();
    if(!session_is_registered(myusername)){
        header("location:main_login.php");
    }
?>
<meta http-equiv="refresh" content="0; url=http://starkwave.com/main/dashboard" />

got the code on an tutorial online and adapted it to my situation i want to echo in a php page the first name of the user that logged in, Thank you very much

Miguel
  • 310
  • 1
  • 5
  • 17
  • `mysql_`is deprecated plz switch to `mysqli_` look at http://stackoverflow.com/questions/1390607/how-could-i-change-this-mysql-to-mysqli – JaMaBing Aug 27 '14 at 15:25
  • `session_register` is also deprecated. Where you got that legacy code from :-S – Daniel W. Aug 27 '14 at 15:28

4 Answers4

3

session_register() and session_unregister() will no longer work in future so don't use them. Use

    $_SESSION['myusername']='give your name';

when you checked if user exists in database then use like this

        $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
        $result=mysql_query($sql);


        $count=mysql_num_rows($result);

        if($count==1){
            $data= mysql_fetch_array($result);
            $_SESSION['usernmae']=$data['name_in_database'];/* put here name of column in database*/
            header("location:login_success.php");
            exit();
        }
        else {
            echo "Wrong Username or Password";
        }

and when you want to print name just use

        echo  $_SESSION['usernmae'];
AlexMA
  • 9,842
  • 7
  • 42
  • 64
RN Kushwaha
  • 2,081
  • 3
  • 29
  • 40
0

To echo the name of the user registered in the session, just read the saved variable in the current session:

echo $_SESSION['myusername'];
0

Well, in the session you register the string "myusername", not the variable $myusername, holding the actual username.

You can do $_SESSION["myusername"] = $myusername;

mariobgr
  • 2,143
  • 2
  • 16
  • 31
0

First you must add user's first name to the session when you is authenticated e.g.

if($count==1){
      session_register("myusername");
      session_register("mypassword"); 
      $_SESSION['firstName'] = "abc"; // set the first name that you get from form or somewhere else
      header("location:login_success.php");
   }

Then you can echo $_SESSION['firstName'] from where you want