PHP login with PDO, doesnt seem to work -
there aren't errors given, reason code doesnt seem work..
here code:
<?php session_start(); require_once('inc/db.php'); if(isset($_session['username'])) { header("location: index.php"); } else { try { $username = mysql_real_escape_string($_post['username']); $password = mysql_real_escape_string($_post['password']); $sql = $dbh->prepare('select * users username =:username , password =:password'); $sql->bindparam(':username', $username); $sql->bindparam(':password', $password); $sql->execute(); $total = $sql->rowcount(); $row = $sql->fetch(); if($total > 0) { if($row['verified'] > 0) { $_session['username'] = $username; } else { echo "unverified"; } } else { echo "incorrect"; } } catch(pdoexception $e) { } } ?>
can me see what's wrong? thank lot in advance :)
edit::::
here db.php
<?php try { $db_name = 'users'; $db_user = 'root'; $db_pass = ''; $dbh = new pdo('mysql:host=localhost;dbname='.$db_name, $db_user, $db_pass, array(pdo::mysql_attr_init_command => "set names utf8")); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch(pdoexception $e) { echo "error?"; } ?>
this should serve better , identify errors if , when occur
<?php // inc/db.php $dbh = new pdo('mysql:host=localhost;dbname=users;charset=utf8', 'root', ''); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); $dbh->setattribute(pdo::attr_emulate_prepares, false);
and main file
<?php // remove these 2 lines when you've finished development ini_set('display_errors', 'on'); error_reporting(e_all); session_start(); if (isset($_session['username'])) { header("location: index.php"); exit; } if (!isset($_post['username'], $_post['password'])) { throw new runtimeexception('required post parameters not present'); } require_once __dir__ . '/inc/db.php'; $sql = $dbh->prepare('select * users username = :username , password = :password'); $sql->bindparam(':username', $_post['username']); $sql->bindparam(':password', $_post['password']); $sql->execute(); $row = $sql->fetch(pdo::fetch_assoc); if ($row !== false) { if ($row['verified'] > 0) { $_session['username'] = $username; // want redirect here or } else { echo 'unverified'; } } else { echo "incorrect"; }
Comments
Post a Comment