Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7732001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:37:20+00:00 2026-06-01T06:37:20+00:00

I need to implement for current project some secure login in php. I wrote

  • 0

I need to implement for current project some secure login in php.
I wrote something with pdo prepared statements with old code having in mind. I need advices to implement more secure maybe, or to examine is it proof to sql inj, attacks.

 <form action="validate.php" method=post>
    <table class="loginForm">
       <thead></thead>
       <tbody>
          <tr>
            <td>UserName:</td>
            <td><input name=user_name></td>
          </tr>
          <tr>
           <td>Pass:</td>
           <td><input type=password name=password></td>
          </tr>
          <tr>
           <td><input class=loginBtn type=submit value='Log me in' name=login></td>
         </tr>
       </tbody>
    </table>
</form>

Validate.php

session_start();    
try {
    $host = 'localhost';
    $dbName = 'xxx';
    $dbUser = 'xxx';
    $dbPass = 'xxx';
    # MySQL with PDO_MYSQL  
    $DBH = new PDO("mysql:host=$host;dbname=$dbName", $dbUser, $dbPass);
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "I'm afraid I can't do that.";
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}

$STH = $DBH->prepare("SELECT * FROM `Admins` WHERE `username` = '$user_name' AND password='$password'");
$STH->execute();

$STH->setFetchMode(PDO::FETCH_ASSOC);
$affected_rows = $STH->fetchColumn();

if($affected_rows == 1) {
    //add the user to our session variables
    $_SESSION['username'] = $user_name;
    header("Location: http://www.mysite.com/administration/index.php");
        exit;
        //print 'allowed';
        }
    else {
        print 'access is not allowed !!!';
    }

Updated:
Thanks to accepted answer bellow my query is coded now like this

$STH = $DBH->prepare('SELECT * FROM Admins 
                      WHERE username = :user and password = :pass');
$STH->execute(array(':user' => $_POST['user_name'], 
                    ':pass' => $_POST['password']));

Which is fine.
Now I just want to be sure is my authentication is good enough to use it on admin pages like this
session_start();

try {
    $host = 'xx';
    $dbName = 'xxxx';
    $dbUser = 'xxxxx_first';
    $dbPass = 'xxx';
    # MySQL with PDO_MYSQL  
    $DBH = new PDO("mysql:host=$host;dbname=$dbName", $dbUser, $dbPass);
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "I'm afraid I can't do that.";
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}

if (empty($_SESSION['username'])) {
    die('To access pages you have to be loged in.
    <a href="/administration/login.php">log in</a> ');

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:37:21+00:00Added an answer on June 1, 2026 at 6:37 am

    You can type query like this to avoid Sql Injection

    $STH = $DBH->prepare('SELECT * FROM Admins 
                          WHERE username = :user and password = :pass');
    $STH->execute(array(':user' => $_POST['user_name'], 
                        ':pass' => $_POST['password']));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement login velocity checking for a web service. The service is
In my current project I'm using classes which implement the following ITransaction interface shown
I'm trying to implement Authorization and Authentication in my current winforms project. The Authentication
In my current project, we need to authenticate users of an ASP.NET application against
I'm looking for some input on my current project architecture. There are three components:
I want to add some feature to this project and I need smartgwt for
For a current project I am working I need to return an aggregate report
I'm doing a project where i need some custom swing components. So far I
How do I implement a save and load dialogue box into my current project?
In my current Spring/JSF project I need integrate a method to push server side

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.