Login in PHP Using PDO Error!

<?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?:sick:

Why my login code is not working?

What is not working and what error do you have ?

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.


error_reporting(E_ALL);
ini_set('display_errors', 1);

Add that to the top of your page then paste the error here.

Try this and you should be able to see what is happening:



<?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?

@msz900; simpler that someone else giving you some code to try?

What error messages did you get?

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,‘:pass’ => $secure));

Try this:


<?php 
error_reporting( -1 );  // 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( $pass, PASSWORD_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


No error will occur… redirect me to the login page

Why?

Try displaying the $result before the test:



  vd( $result->fetchColumn(), __LINE__ );
  if($result->fetchColumn()>0)
  {
    ...
  }


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 
error_reporting( -1 );  // 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

i got it…

The Update code is…

<?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…