Username:
Password:
Remember me:
Register

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)


Search forums via Google


4 Users appreciate this thread.

Sample Login Script
Started by ryanb000977
(2014-05-15 20:44:57)
ryanb000977 (2014-05-15 20:44:57)
Hello, this script uses the database information in my other latest script. So, here is the code...

Source Code
<?php session_start(); $logged = $_SESSION['username']; if(!empty($logged)) { header('Location: index.php'; } $con = mysqli_connect("host","user","pass","db" ); $username = $_POST['username']; $username = mysqli_real_escape_string($con, $username); $username = strip_tags($username); $username = str_replace(' ','',$username); $password = $_POST['password']; $password = mysqli_real_escape_string($con, $password); $password = htmlspecialchars($password, ENT_QUOTES); $password = hash('sha256',$password); $ip = $_SERVER['REMOTE_ADDR']; $select = mysqli_query($con, "SELECT * FROM users WHERE username='$username' and password='$password'" ); $count = mysqli_num_rows($select); if($username !== '' && $password !== '' && $count === 1) { $_SESSION['username'] = $username; $msg = "You are logged in as " . $username . ". <a href=\"home.php\">Home</a>."; $form = 0; } elseif($count === 0) { $msg = "Invalid username and/or password!"; $form = 1; } else { $msg = "Please make sure all fields are filled in!"; $form = 1; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> <style type="text/css"> body { background-color: red; } #main { background-color: white; border: 2px solid black; text-align: center; } </style> </head> <body> <div id="main"> <center> <h1>Login</h1> <?php if($msg !== '' { echo("<p>" . $msg . "</p>" ); } if($form === 1) { echo("<form method=\"post\" action=\"login.php\"> <table> <tbody> <tr> <td>Username:</td> <td><input name=\"username\" type=\"text\" value=\"\"></td> </tr> <td>Password:</td> <td><input name=\"password\" type=\"password\" value=\"\"></td> </tr> <td></td> <td><input name=\"submit\" type=\"submit\" value=\"Register\"></td> </tr> </tbody> </table> </form>" ); } ?> </center> </body> </html>


This post has been edited one or more times, the last time was:
2014-05-17 03:59:38

angelrulez7 (2014-05-16 01:28:15)
You should make it so that if the inserted username exists, it only returns "Incorrect password!"
a
TwilightWinter (2014-07-19 18:56:38)
All of the users that comment here are experienced, so can someone make a thread about, "How to set up PHP environment". The installation of PHP and Apache Server and how to get it working.

This post has been edited one or more times, the last time was:
2014-07-20 01:57:42

✋
joemckellar20 (2014-08-26 05:54:50)
really look it up online
1999 (2016-12-30 06:26:48)
Understand that this is old but the passwords should be hashed and salted. And have email/username as login
squirtle2016 (2017-06-03 17:55:28)
This is gay and so are you. I have your address by the way.
Stop, or you die.
TaigaFanboy (2017-08-03 11:53:56)
Do not use this, passwords are hashed without a salt, which makes it easy for someone who managed to get into your system to use rainbow tables to figure out user passwords.
 

Log in to submit a comment

This topic's ID: 69795

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)




Total registered users: 8321
New registered users today: 7
Newest registered user: ElegantVulpes

©  Copyright 2026 3DSPlaza. All Rights Reserved