Session problem

Hi, I need some help in this session,why i could not get the name or put some session when the user login…but i can successfully login and when i tried to echo the user it is empty…can you please help me on this.

This is my code.

Loginform.php //I use ajax in requesting


    $(function(){
           $('#btn').click(function(e){
               e.preventDefault();
               var serializeform = $('#myform').serialize();
               $.ajax({
                   type: 'post',
                   url: 'tologin.php',
                   data: serializeform,
                   success: function(data){
                       if(data == 'success'){
                           window.location.href = 'success.php';
                       }
                      

                   }
               });

           });
       });



  <?php
    include_once('connection.php');

     if(isset($_POST['password']) && isset($_POST['username'])){
           $username = $_POST['username'];
           $password = $_POST['password'];


           $message= userlogin($username,$password);

           echo $message;
    }

?>


connection.php


  function userlogin($username,$password)
{
    try {
       .....................
       ......................
       ......................

      select statement here
       .............................................
       .........................................


        if ($cm->rowCount() == 1) {

            $row=$cm->fetch(PDO::FETCH_OBJ);

            $_SESSION['username']=$row->username;
            $_SESSION['password']=$row->password;

            return "success";
        }
     
    } catch (PDOException $exception) {
        return "Failed"
    }
}




success.php


    session_start();
    $user='';
      if(isset($_SESSION['username'])){
          $user = $_SESSION['username'];
      }

    echo "user";

user is empty i don’t know why.

Please help me Thank you in avdvance.

Change this code slightly and see if you get different results


  session_start();
    $user='**NO USER**';
      if(isset($_SESSION['username'])){
          $user = $_SESSION['username'];
      }

    echo "user";  

This will determine if the username is not being set in the session or the it is set but blank.

Hi thank you for the reply…i tried it will display NO USER ,how can fixed this ?

Hi can i ask?why is it my username is set blank?..does it means it conflict to other variable,maybe i have another username variable?

Please help me

Thank you in advance.

That little test demonstrates that the SESSION does not exist or does not contain the ‘username’.

This bit of code on your other page may not be getting called:


if(isset($_POST['password']) && isset($_POST['username']))

It requires BOTH a username AND a password. Perhaps you are not validating that both fields are being filled by the user.

Additionally, your SELECT statement may be returning more than one record. The code you showed here will populate the SESSION ONLY if a single record is returned.

Without a little more detail there is not much more anyone can do to help you. These are all guesses based on the snippets of code you have provided.

Hi thank you PrkinT,… i will try to find way how to resolve this problem

Hi Parkin T, I tried to change to put session on the password and it’s working the user’s value is the password and it display as the user,…i don’t know why it works in my password.maybe i have problem naming my variable username.

Hi jemz,

Are you calling session_start() at the top of your tologin.php file?

Hi fretburner Thank you so much you solved my problem…many regards :slight_smile: