Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------------------------------------------------------------
- PHP (databaseHandler.php):
- ---------------------------------------------------------------------------------------------------------------------------------------
- <?php
- $databaseServerName = "localhost";
- $databaseUserName = "root";
- $databasePassword = "";
- $databaseName = "users";
- $conn = mysqli_connect($databaseServerName, $databaseUserName, $databasePassword, $databaseName);
- ?>
- ---------------------------------------------------------------------------------------------------------------------------------------
- HTML (game.php): (Security is bad, I know....) (I am attempting to pass the form data through to the PHP files which checks it and either confirms the login or adds a new user to the database. However I also need the variables to store the username etc as well as some more data which I need to pull from the database (when I have created it) and keep that in the javascript variables too. If that makes sense. However, at the moment, the code stores the users form inputs in the javascript variables, but the PHP isn't run - you can login with any username & password and a new user isn't pushed to the database. What am I doing wrong?
- ---------------------------------------------------------------------------------------------------------------------------------------
- <?php
- //start a session
- session_start();
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <link rel="Stylesheet" type="text/css" href="style.css"></link>
- <script src="jquery.js"></script>
- <script>
- $(document).ready(function(){
- $("#login").click(function(){
- let username = $("#username").val();
- let password = $("#password").val();
- $.post("login.php", {
- username: username,
- password: password,
- loggedIn: true
- }, function(data, status){
- $("div").html(username);
- });
- });
- $("#signUp").click(function(){
- let username = $("#username").val();
- let password = $("#password").val();
- $.post("signUp.php", {
- username: username,
- password: password,
- loggedIn: true
- }, function(data, status){
- $("div").html(username);
- });
- });
- $("#playAsGuest").click(function(){
- let loggedIn = false;
- });
- });
- </script>
- </head>
- <body>
- <img id="logo" src="Logo.png" alt="Escape From Death Logo" width="100%" height="33%">
- <h1>WELCOME</h1>
- <div>
- <form>
- <input id="username" type="text" name="username" placeholder="Username">
- <br>
- <input id="password" type="password" name="password" placeholder="Password">
- <br>
- <button id="login" type="button" name="signup">LOGIN</button>
- <button id="signUp" type="button" name="signup">SIGN UP</button>
- <button id="playAsGuest" type="button" name="playAsGuest">PLAY AS GUEST</button>
- </form>
- <div>
- </body>
- </html>
- ---------------------------------------------------------------------------------------------------------------------------------------
- PHP (login.php)
- ---------------------------------------------------------------------------------------------------------------------------------------
- <?php
- //start a session
- session_start();
- //If the button is clicked
- //if (isset($_POST['username']) && isset($_POST['password'])){
- //The connection to the database
- include_once 'databaseHandler.php';
- //Gets the infomation submitted from the form
- //Protects the database by converting everything to text...
- //The database therefore cannot read the inputs as code
- $username = mysqli_real_escape_string($conn, $_POST['username']);
- $password = mysqli_real_escape_string($conn, $_POST['password']);
- $loggedIn = mysqli_real_escape_string($conn, $_POST['loggedIn']);
- //Error Handlers
- //Checks everything has been filled out
- //Checks for no empty fields
- if (empty($username) || empty($password)){
- //Takes you back to this page
- //With a message at the end of the url
- //header("Location: loginHome.php?login=empty");
- exit();
- }
- else {
- //Checks that username is in the database
- $sql1 = "SELECT * FROM login_details WHERE username = '$username';";
- $result = mysqli_query($conn, $sql1);
- $resultCheck = mysqli_num_rows($result);
- if ($resultCheck < 1){
- //Takes you back to this page
- //With a message at the end of the url
- //header("Location: loginHome.php?login=error1");
- exit();
- }
- else {
- //Checks username with password
- if ($row = mysqli_fetch_assoc($result)){
- //De-hashing the password
- $hashedPasswordCheck = password_verify($password, $row['password']);
- if ($hashedPasswordCheck == false){
- // header("Location: loginHome.php?login=error2");
- exit();
- }
- else if ($hashedPasswordCheck == true){
- //Log in the user here
- //Use Sessions
- $_SESSION['u_id'] = $row['user_id'];
- $_SESSION['u_username'] = $row['username'];
- $_SESSION['u_password'] = $row['password'];
- echo $username;
- echo $password;
- echo $loggedIn;
- // header("Location: game.php?login=success");
- // exit();
- }
- }
- }
- }
- // }
- // else {
- // // header("Location: loginHome.php");
- // exit();
- // }
- ?>
- ---------------------------------------------------------------------------------------------------------------------------------------
- PHP (signUp.php)
- ---------------------------------------------------------------------------------------------------------------------------------------
- <?php
- //start a session
- session_start();
- if (isset($_POST['signup'])){
- //The connection to the database
- include_once 'databaseHandler.php';
- //Gets the infomation submitted from the form
- //Protects the database by converting everything to text...
- //The database therefore cannot read the inputs as code
- $username = mysqli_real_escape_string($conn, $_POST['username']);
- $password = mysqli_real_escape_string($conn, $_POST['password']);
- $loggedIn = mysqli_real_escape_string($conn, $_POST['loggedIn']);
- //Error Handlers
- //Checks everything has been filled out
- //Checks for no empty fields
- if (empty($username) || empty($password)){
- //Takes you back to this page
- //With a message at the end of the url
- //header("Location: signUpHome.php?signup=empty");
- //exit();
- }
- else {
- //Checks that input is valid
- if (!preg_match("/[a-zA-Z0-9]/", $username) || !preg_match("/[a-zA-Z0-9]/", $password)){
- //Takes you back to this page
- //With a message at the end of the url
- //header("Location: signUpHome.php?signup=invalid");
- //exit();
- }
- else {
- //Checks that username hasn't already been taken
- $sql1 = "SELECT * FROM login_details WHERE username = '$username';";
- $result = mysqli_query($conn, $sql1);
- $resultCheck = mysqli_num_rows($result);
- if ($resultCheck > 0){
- //Takes you back to this page
- //With a message at the end of the url
- //header("Location: signUpHome.php?signup=usertaken");
- //exit();
- }
- else {
- //Hashing the password
- $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
- //Insert the user into the database
- $sql2 = "INSERT INTO login_details (username, password) VALUES ('$username','$hashedPassword');";
- mysqli_query($conn, $sql2);
- // $row = mysqli_fetch_assoc($result);
- // $_SESSION['u_id'] = $row['user_id'];
- // $_SESSION['u_username'] = $row['username'];
- // $_SESSION['u_password'] = $row['password'];
- echo $username;
- echo $password;
- echo $loggedIn;
- //Takes you back to this page
- //header("Location: game.php?signup=success");
- //exit();
- }
- }
- }
- }
- else {
- //header("Location: signUpHome.php");
- //exit();
- }
- ?>
- ---------------------------------------------------------------------------------------------------------------------------------------
- CSS (style.css):
- ---------------------------------------------------------------------------------------------------------------------------------------
- body {
- background-color: #443838;
- }
- h1 {
- position: fixed;
- font-family: "impact";
- color: #FF0000;
- font-size: 50px;
- top: 50%;
- left: 50%;
- margin-top: -70px;
- margin-left: -550px;
- }
- #username{
- position: fixed;
- width: 20%;
- height: 5%;
- border: none;
- border-radius: 10px;
- top: 50%;
- left: 50%;
- margin-top: 0px;
- margin-left: -550px;
- }
- #password{
- position: fixed;
- width: 20%;
- height: 5%;
- border: none;
- border-radius: 10px;
- top: 50%;
- left: 50%;
- margin-top: 50px;
- margin-left: -550px;
- }
- #login{
- position: fixed;
- height: 7%;
- border: none;
- border-radius: 10px;
- top: 50%;
- left: 50%;
- margin-top: +100px;
- margin-left: -550px;
- font-family: "impact";
- font-size: 30px;
- text-align: left;
- }
- #signUp{
- position: fixed;
- height: 7%;
- border: none;
- border-radius: 10px;
- top: 50%;
- left: 50%;
- margin-top: +100px;
- margin-left: -450px;
- font-family: "impact";
- font-size: 30px;
- text-align: left;
- }
- #playAsGuest{
- position: fixed;
- height: 7%;
- border: none;
- border-radius: 10px;
- top: 50%;
- left: 50%;
- margin-top: 160px;
- margin-left: -550px;
- font-family: "impact";
- color: #FFFFFF;
- background-color: #000000;
- font-size: 25px;
- text-align: left;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement