Hey there! any question in your mind? Ask It Now!.

Popular Categories

How to use login script in PHP and PDO ?

+6 votes
asked in Programming by Hafizul Fitri (410 points)
retagged by Bhartesh
Hello below is my register code. I am very new to PHP and PDO. I would very much like to learn it so if you could be so kind to answer my question which is how do you create a login form with what I got from the code below:

Your cooperation and help will be most appreciated.
// database connection
        DB::query("INSERT INTO users(fullname,username,pass) values (?,?,?)",array($_POST["fullname"],$_POST["username"],$_POST["pass"]));
    }else {
    echo "Yay";
    <link rel="stylesheet" type="text/css" href="style2.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name='registration' action="please_work.php" method="post">
     <input type="text" name="fullname" />
     <input type="text" name="username" />
     <input type="password" name="pass" />
        <input type="submit" name="submit" value="submit">

3 Answers

+3 votes
answered by Himanshu Expert (5,940 points)
selected by Hafizul Fitri
Best answer

May be this code help you to complte log in script in PDO.

$errmsg_arr = array();
$errflag = false;
// configuration
$dbhost = "localhost";
$dbname = "pdo_ret";
$dbuser = "root";
$dbpass = "";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$user = $_POST['uname'];
$password = $_POST['pword'];
if($user == '') {
$errmsg_arr[] = 'You must enter your Username';
$errflag = true;
if($password == '') {
$errmsg_arr[] = 'You must enter your Password';
$errflag = true;
// query
$result = $conn->prepare("SELECT * FROM users WHERE username= :hjhjhjh AND password= :asas");
$result->bindParam(':hjhjhjh', $user);
$result->bindParam(':asas', $password);
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
header("location: home.php");
$errmsg_arr[] = 'Username and Password are not found';
$errflag = true;
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
header("location: index.php");
Enjoy! :)
commented by Hafizul Fitri (410 points)
Ahhh this is the one I've been looking for thanks man
commented by Himanshu Expert (5,940 points)
pleasure of mine dear :)
commented by Bhartesh (2,730 points)
good work this script helped me also.
+2 votes
answered by nisha (720 points)

If you are asking for log in code for this So it may help you see this code: 


$user = $_POST['user']; //Set UserName
$pswrd = $_POST['pswrd']; //Set Password
if(isset($user, $pswrd)) {
    include('/config.php'); //Initiate the MySQL connection
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($user);
    $mypassword = stripslashes($pswrd);
    $myusername = mysqli_real_escape_string($dbC, $myusername);
    $mypassword = mysqli_real_escape_string($dbC, $mypassword);
    $sql="SELECT * FROM studetail WHERE name='$myusername' and pass='$mypassword'";
    $result=mysqli_query($dbC, $sql);
    // Mysql_num_row is counting table row
    // If result matched $myusername and $mypassword, table row must be 1 row
        // Register $myusername, $mypassword and redirect to file "admin.php"
        $_SESSION['name']= $myusername;
<script type="text/javascript" language="javascript">
alert("User name or password not corroct");
<?php }
In this code we are matching the username and password from database and counting result , on the basis of result we are redirecting to home page or index page. I hope this will help you.
commented by Hafizul Fitri (410 points)
Thank you for your time answering this question although it may not be PDO because  I can see mysqli in the code but thank you any PHP knowledge will do :D
+1 vote
answered by duke Expert (5,813 points)

Hello Hafizul I am giving u a sample code change it accordingly, It may help you in PDO. 

See the code: 

    function dbConnect(){
        $username = 'root';
        $password = '';
        $conn = new pdo("mysql:host=localhost;dbname=test;",$username,$password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         return $conn;
        }   catch(PDOException $e){
            echo 'ERROR', $e->getMessage();
you can change it accordingly . i hope you got this.
commented by duke Expert (5,813 points)
@Hafizul does it answer your question?
commented by Ole
This is not a log in script. This just shows how to connect to a database using PDO
commented by Hafizul Fitri (410 points)
not really bro but thanks for trying :D appreciate your effort now lets code :P

Related Questions

+4 votes
5 answers 756 views
0 votes
1 answer 9,887 views
+2 votes
3 answers 303 views
asked in Programming by jatin Expert (3,823 points)
+2 votes
1 answer 171 views
+4 votes
1 answer 653 views
+6 votes
2 answers 1,637 views
+6 votes
1 answer 254 views
+1 vote
2 answers 105 views
0 votes
1 answer 164 views
+1 vote
1 answer 241 views
asked in Programming by duke Expert (5,813 points)

Not a Member yet?

Ask to Folks Login

My Account
517 Folks are online
5 members and 512 guest online
Your feedback is highly appreciated