Last active
December 19, 2021 12:55
-
-
Save erowsika/5dd4b64ac36eef804bb82b987e4c144e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // file connection.php | |
| $con = mysqli_connect("localhost", "username", "password", "dbname"); | |
| // file login.php | |
| require_once(__DIR__ . '/connection.php'); | |
| session_start(); | |
| // mysqli_real_escape_string prevent sql injection (security) | |
| $user = mysqli_real_escape_string($con, $_POST['username']); | |
| $pass = mysqli_real_escape_string($con, $_POST['password']); | |
| //convert password to md5 | |
| $pass = md5($pass); | |
| // check if the user id and password combination exist in database | |
| $sql = mysqli_query($con, "SELECT * FROM table WHERE username = '$user' AND password = '$pass'") or die(mysqli_error($con)); | |
| //if match is equal to 1 there is a match | |
| if (mysqli_num_rows($sql) === 1) { | |
| $user = mysqli_fetch_array($sql); | |
| //set session | |
| $_SESSION['authorized'] = true; | |
| $_SESSION['username'] = $user['username']; // username stored in session global variable | |
| // reload the page | |
| $_SESSION['success'] = 'Login Successful'; | |
| header('Location: ./index.php'); | |
| exit; | |
| } else { | |
| // login failed save error to a session | |
| $_SESSION['error'] = 'Sorry, wrong username or password'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment