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

Popular Categories





How to use login script in PHP and PDO ?

+6 votes
6,732 views
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.
 
 
<?php
// database connection
require_once("DB.php");
DB::setup("pggmb","root","");
$error=array();
session_start();
 
 
if(isset($_POST["fullname"],$_POST["username"],$_POST["pass"])){
    if($_POST["fullname"]&&$_POST["pass"]){
 
        DB::query("INSERT INTO users(fullname,username,pass) values (?,?,?)",array($_POST["fullname"],$_POST["username"],$_POST["pass"]));
    }else {
 
    }
    echo "Yay";
}
 
?>
 
<html>
    <head>
    <link rel="stylesheet" type="text/css" href="style2.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <body>
    <center><h1>Register</h2></center>
 
<form name='registration' action="please_work.php" method="post">
     <label>Fullname:</label><br>
     <input type="text" name="fullname" />
     <br>
     <label>Username:</label><br>
     <input type="text" name="username" />
     <br>
      <label>Password:</label><br>
     <input type="password" name="pass" />
     <br>
        <input type="submit" name="submit" value="submit">
 </form>
 </body>
</html>
 

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.

<?php
session_start();
$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);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
header("location: home.php");
}
else{
$errmsg_arr[] = 'Username and Password are not found';
$errflag = true;
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: index.php");
exit();
}
 
?>
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,630 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: 

 

<?php
error_reporting(0);
$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
    $count=mysqli_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
        // Register $myusername, $mypassword and redirect to file "admin.php"
        session_register("hi");
        session_register("password");
        $_SESSION['name']= $myusername;
        header("location:home.php");
}
else
{?>
<script type="text/javascript" language="javascript">
alert("User name or password not corroct");
window.location.href='index.php';
</script>
<?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: 

   <?php
    function dbConnect(){
       try{
        $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

+2 votes
3 answers 258 views
+2 votes
1 answer 121 views
+4 votes
1 answer 587 views
+6 votes
2 answers 1,165 views
0 votes
1 answer 132 views
+1 vote
2 answers 57 views

Not a Member yet?

Ask to Folks Login

My Account
263 Folks are online
1 members and 262 guest online