SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: session problem

  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post 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

    Code:
        $(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 Code:
      <?php
        
    include_once('connection.php');

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


               
    $messageuserlogin($username,$password);

               echo 
    $message;
        }

    ?>
    connection.php

    Code:
      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

    PHP Code:
        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.

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Change this code slightly and see if you get different results
    PHP Code:
      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.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ParkinT View Post
    Change this code slightly and see if you get different results
    PHP Code:
      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 ?

  4. #4
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ParkinT View Post
    Change this code slightly and see if you get different results
    PHP Code:
      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 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.

  5. #5
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    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:
    PHP Code:
    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.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi thank you PrkinT,.. i will try to find way how to resolve this problem

  7. #7
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ParkinT View Post
    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:
    PHP Code:
    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 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.

  8. #8
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,440
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi jemz,

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

  9. #9
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •