SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    15
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Login in PHP Using PDO Error!

    Code:
    <?php session_start();
    include 'conn.php';
    if(isset($_POST) && count($_POST)>0) {
    try
    {
        $user=$_POST['user'];
        $pass = $_POST['pass'];
        $secure=password_verify($pass,PASSWORD_DEFAULT);
        $query = "SELECT * FROM signup WHERE Username=:users AND Password=:pass";
        $result=$conn->prepare($query);
        $result->execute(array('users'=>$user,':pass'=>$secure));
        if($result->fetchColumn()>0)
        {
            // Set username session variable
            $_SESSION['user'] = $user;
            // Jump to secured page
            header('location:index.php');
        }
        else
        {
            header("location:signin.php");
        }
    }
    catch(PDOException $e){
            echo 'ERROR', $e->getMessage();
        }
    
    }
    
    ?>
    Why my login code is not working?

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Why my login code is not working?
    What is not working and what error do you have ?

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    15
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The login code is not working and it will show no error, the main issue is that it will not login...

  4. #4
    SitePoint Addict bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    277
    Mentioned
    8 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by msz900 View Post
    The login code is not working and it will show no error, the main issue is that it will not login...
    I laughed harder at this than I should have. lol

    For real though, that doesn't help at all. If you want someone to help you with the code, you need to include the actual errors. Otherwise they are just looking for a typo or a syntax issue.

    Code:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    Add that to the top of your page then paste the error here.

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,890
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by msz900 View Post
    The login code is not working and it will show no error, the main issue is that it will not login...
    Try this and you should be able to see what is happening:

    PHP Code:

    <?php 
    error_reportin
    (-1); // maximum error reporting 
    ini_set('display_errors'true);

    session_start();
    // include 'conn.php';
    require 'conn.php';
    if(isset(
    $_POST) && count($_POST)>0)
    {
        try
        {
    vd($_POST__LINE__);
            
    $user   $_POST['user'];
            
    $pass   $_POST['pass'];
            
    $secure password_verify($pass,PASSWORD_DEFAULT);
            
    $query  "SELECT * FROM signup WHERE Username=:users AND Password=:pass";
    vd($query__LINE__);

            
    $result $conn->prepare($query);
            
    $result->execute(array('users'=>$user,':pass'=>$secure));
            if(
    $result->fetchColumn()>0)
            {
                
    // Set username session variable
                
    $_SESSION['user'] = $user;
                
    // Jump to secured page
                
    header('location:index.php');

            }else{
                
    header("location:signin.php");
            }
    //endtry

        
    }catch(PDOException $e){
           echo 
    'ERROR'$e->getMessage();
        }

    }
    //endif

    //================================================
    //  Debug
    //  usage:
    //             vd($val);
    //================================================
    function vd($val='$val Must be NULL'$line=0$print=TRUE)
    {
        echo 
    '<pre>';
            echo 
    '$line = ' .$line;
            if( 
    $print )
            {
               
    print_r($val);
            }else{
               
    var_dump($val);
           }
        echo 
    '</pre>';
    }
    Please note:
    1. the above script has not been tested because of hard-drive failure and still have to instal Localhost, etc, etc, etc, etc, etc
    2. header redirect will not work because of screen output from vd($val, __LINE__);
    3. remove vd($val, __LINE__); once errors eliminated.
    Last edited by John_Betong; Apr 25, 2014 at 19:20. Reason: spelling, not my fortay
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  6. #6
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    15
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by John_Betong View Post
    Try this and you should be able to see what is happening:

    PHP Code:

    <?php 
    error_reportin
    (-1); // maximum error reporting 
    ini_set('display_errors'true);

    session_start();
    // include 'conn.php';
    require 'conn.php';
    if(isset(
    $_POST) && count($_POST)>0)
    {
        try
        {
    vd($_POST__LINE__);
            
    $user   $_POST['user'];
            
    $pass   $_POST['pass'];
            
    $secure password_verify($pass,PASSWORD_DEFAULT);
            
    $query  "SELECT * FROM signup WHERE Username=:users AND Password=:pass";
    vd($query__LINE__);

            
    $result $conn->prepare($query);
            
    $result->execute(array('users'=>$user,':pass'=>$secure));
            if(
    $result->fetchColumn()>0)
            {
                
    // Set username session variable
                
    $_SESSION['user'] = $user;
                
    // Jump to secured page
                
    header('location:index.php');

            }else{
                
    header("location:signin.php");
            }
    //endtry

        
    }catch(PDOException $e){
           echo 
    'ERROR'$e->getMessage();
        }

    }
    //endif

    //================================================
    //  Debug
    //  usage:
    //             vd($val);
    //================================================
    function vd($val='$val Must be NULL'$line=0$print=TRUE)
    {
        echo 
    '<pre>';
            echo 
    '$line = ' .$line;
            if( 
    $print )
            {
               
    print_r($val);
            }else{
               
    var_dump($val);
           }
        echo 
    '</pre>';
    }
    Please note:
    1. the above script has not been tested because of hard-drive failure and still have to instal Localhost, etc, etc, etc, etc, etc
    2. header redirect will not work because of screen output from vd($val, __LINE__);
    3. remove vd($val, __LINE__); once errors eliminated.
    D
    Dear Any simple method please?

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,264
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    @msz900 ; simpler that someone else giving you some code to try?

    What error messages did you get?

  8. #8
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    15
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    this is the message
    Call to undefined function vd() in G:\xammp\htdocs\leave_system_pdo\public\login.php on line 6

  9. #9
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,890
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by msz900 View Post
    this is the message
    Call to undefined function vd() in G:\xammp\htdocs\leave_system_pdo\public\login.php on line 6
    Missing function occurs on line 6 so it looks as though the supplied script was not copied and pasted correctly?

    Also // ADDED MISSING : BEFORE users // $result->execute(array(':users' => $user,'ass' => $secure));

    Try this:
    PHP Code:
    <?php 
    error_reporting
    ( -);  // MAXIMUM ERROR REPORTING
    ini_set('display_errors'true);

    session_start();

    vd$_POST__LINE__ );  // REMOVE WHEN ERRORS ELIMINATED

    include 'conn.php';

    if(isset(
    $_POST) && count($_POST)>0)
    {
      try
      {
        
    $user   $_POST['user'];
        
    $pass   $_POST['pass'];
        
    $secure password_verify$passPASSWORD_DEFAULT );
        
    $query  "SELECT * FROM signup WHERE Username=:users AND Password=:pass";
    vd($query__LINE__); // REMOVE WHEN ERRORS ELIMINATED

        
    $result $conn->prepare($query);
      
    // ADDED MISSING : BEFORE users
        
    $result->execute(array(':users' => $user,':pass' => $secure));

        
    // DEFAULT FAILURE PAGE
        
    $page 'signin.php';
        if(
    $result->fetchColumn()>0)
        {
          
    // Set username session variable
          
    $_SESSION['user'] = $user;
          
    // Jump to secured page
          
    $page 'index.php';
        }
    //endif
        
    header('Location: ' .$page);

      }catch(
    PDOException $e){
          echo 
    'ERROR'$e->getMessage();

      }
    //endtry
    }//endif

    //========================================================
    function vd($val='$val Must be NULL'$line=0$print=TRUE)
    {
      
    $style 'clear:both; width:88%; margin:auto; background-color:#000; color:#fff ; padding:1em';  
      echo 
    '<pre style="' .$style .'">';
        echo 
    '$line = ' .$line .'<br />';
        if( 
    $print )
        {
           
    print_r($val);
        }else{
           
    var_dump($val);
        }
      echo 
    '</pre>';

    // ? > // NOT REQUIRED and reduces potential white-space problems
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  10. #10
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    15
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No error will occur...... redirect me to the login page

  11. #11
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,890
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by msz900 View Post
    No error will occur...... redirect me to the login page
    Why?

    Try displaying the $result before the test:
    PHP Code:

      vd
    $result->fetchColumn(), __LINE__ );
      if(
    $result->fetchColumn()>0)
      {
        ...
      } 
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  12. #12
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    15
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    still nothing happens...

  13. #13
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,890
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by msz900 View Post
    still nothing happens...
    In order to give some assistance it is essential that more information is given:

    1. have you a blank screen?
    2. if anything is displayed on the screen then please show results.
    3. show contents of the $newErrorLog file.

    Try adding this to the top of your page:
    PHP Code:

    <?php 
    error_reporting
    ( -);  // MAXIMUM ERROR REPORTING
    ini_set('display_errors'true);

    $newErrorLog getcwd() .'/NEW_TEMPORARY_ERROR_LOG.PHP'
    ini_set'error_log'$newErrorLog );
    ...
    ...
    ...

    // show contents of $newErrorLog
    highlight_file$newErrorLog );

    // End of file
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  14. #14
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    15
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i got it....

    The Update code is....

    Code:
    <?php
        include 'connection.php';
        try
        {
        $user=$_POST['user'];
        $pass=$_POST['password'];
        $secpss=password_hash($pass,PASSWORD_DEFAULT);
        $smt=$conn->prepare("SELECT * FROM user WHERE User='$user'");
        $smt->execute();
        $result=$smt->fetch(PDO::FETCH_OBJ);
        $prev=$result->Password;
        if(password_verify($pass,$prev))
        {
            header('location:welcome.php');
        }
        else
        {
            header('location:signin.php');
        }
        }
        catch(ErrorException $e)
        {
            $e->getMessage();
        }
        ?>
    this is the correct process,, and the main thing is that mostly we could make a simple mistake like coma(,) or data base column length. In my case i thing that i could provide a correct length of password but accedntly it was a short length, i correct it and follow the php 5.5 documentation and the problem is solved..
    By the way thanks all of your replay...


Tags for this Thread

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
  •