U bekijkt het onderwerpartikel [Top] 136 PHP Login system not working dat door ons is samengesteld uit vele bronnen op internet.
Yes, I deleted both, thanks for that @droopsnoot and @spaceshiptrooper!
What about the code you have? Does it look like this?
$sql = "INSERT INTO Users (Naam, wachtwoord) VALUES (?, ?)";
This is the correct way of doing it because you have 2 columns you want to insert data into. So you are required to use 2 ?
.
Can we get a screenshot of just the table rows in your database? Don’t take a capture of the URL
.
This is what I’m seeing. I used very minimum changes and got yours to work. Not entirely sure why you can’t get it to work. Are you just using codeanywhere
to code your files?
Yes, I only use codeanywhere, but your code is almost exactly the same as mine and yours works. Wierd
I have no idea either. I tried to replicate the problem, but it seems to just insert perfectly for me. Even on 000webhost
. Are you sure there isn’t something you missed or updated? Can you post the full code you have currently to here? Please wrap the codes using the </>
icon as well.
Well, it is as it is.
But I have another question actually, if I go to the login screen, and I log in with the name and password that are in the database, I don’t get forwarded to the welkom.php, but I get forwarded to a page that says login.php but has everything of the index.php in it.
Can you maybe help @spaceshiptrooper?
My code:
<!DOCTYPE html> <?php //session_start(); $link = mysqli_connect("localhost","id5386627_root","XXXxxx","id5386627_root"); if($link === false){ die("ERROR: Kon niet verbinden. " . mysqli_connect_error()); } if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){ header("location: welkom.php"); exit; } $username = $password = ""; $username_err = $password_err = ""; $param_username = $username ; $param_password = password_hash($password, PASSWORD_DEFAULT); if($_SERVER["REQUEST_METHOD"] == "POST"){ if(empty(trim($_POST["username"]))){ $username_err = "Voer hier uw gebruikersnaam in."; } else{ $username = trim($_POST["username"]); } if(empty(trim($_POST["password"]))){ $password_err = "Voer uw wachtwoord in."; } else{ $password = trim($_POST["password"]); } if(empty($username_err) && empty($password_err)){ $sql = "SELECT Naam, wachtwoord FROM Users WHERE Naam = ?"; if($stmt = mysqli_prepare($link, $sql)){ mysqli_stmt_bind_param($stmt, 's', $param_username); $param_username = $username; if(mysqli_stmt_execute($stmt)){ mysqli_stmt_store_result($stmt); if(mysqli_stmt_num_rows($stmt) == 1){ mysqli_stmt_bind_result($stmt, $username, $hashed_password); if(mysqli_stmt_fetch($stmt)){ if(password_verify($password, $hashed_password)){ session_start(); $_SESSION["loggedin"] = true; $_SESSION["password"] = $password; $_SESSION["username"] = $username; header("location: welkom.php"); exit; } else{ $password_err = "Het wachtwoord bestaat niet."; } } } else{ $username_err = "Geen account gevonden met deze gebruikersnaam."; } } else{ echo "Er is iets misgegaan, probeer het later opnieuw."; } } } } ?> <html> <head> <meta charset="UTF-8"> <title>Inloggen</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"> <style type="text/css"> body { font: 14px sans-serif; } .wrapper { width: 350px; padding: 20px; } </style> </head> <body> <center> <div class="wrapper"> <h2>Login</h2> <p>Voer uw gegevens in om door te gaan.</p> <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"> <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>"> <label>Gebruikersnaam</label> <input type="text" name="username" class="form-control" value="<?php echo $username; ?>"> <span class="help-block"><?php echo $username_err; ?></span> </div> <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>"> <label>Wachtwoord</label> <input type="password" name="password" class="form-control"> <span class="help-block"><?php echo $password_err; ?></span> </div> <div class="form-group"> <input id="button" type="submit" class="btn btn-primary" value="Verzenden"> <input id="button" type="reset" class="btn btn-default" value="Leeg maken"> </div> <p>Heb je nog geen account? <a href="register.php">Meld je hier aan</a>.</p> </form> </div> </center> </body> </html>
Hi, @1000prosent, I’ve reformatted your code for you, because the single backtick before and after only work for inline code. For larger blocks of code, you need to place three (3) backticks on the line before the code, and three backticks on the line after the code.
When I log in on the login.php page with the values in the database, I don’t get forwarded to welkom.php, but to a page that still has login.php in the title and all the code from the index.php.
Can any of you please help?
The code from login.php:
<!DOCTYPE html> <?php //session_start(); $link = mysqli_connect("localhost","id5386627_root","XXXxxx","id5386627_root"); if($link === false){ die("ERROR: Kon niet verbinden. " . mysqli_connect_error()); } if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){ header("location: welkom.php"); exit; } $username = $password = ""; $username_err = $password_err = ""; $param_username = $username ; $param_password = password_hash($password, PASSWORD_DEFAULT); if($_SERVER["REQUEST_METHOD"] == "POST"){ if(empty(trim($_POST["username"]))){ $username_err = "Voer hier uw gebruikersnaam in."; } else{ $username = trim($_POST["username"]); } if(empty(trim($_POST["password"]))){ $password_err = "Voer uw wachtwoord in."; } else{ $password = trim($_POST["password"]); } if(empty($username_err) && empty($password_err)){ $sql = "SELECT Naam, wachtwoord FROM Users WHERE Naam = ?"; if($stmt = mysqli_prepare($link, $sql)){ mysqli_stmt_bind_param($stmt, 's', $param_username); $param_username = $username; if(mysqli_stmt_execute($stmt)){ mysqli_stmt_store_result($stmt); if(mysqli_stmt_num_rows($stmt) == 1){ mysqli_stmt_bind_result($stmt, $username, $hashed_password); if(mysqli_stmt_fetch($stmt)){ if(password_verify($password, $hashed_password)){ session_start(); $_SESSION["loggedin"] = true; $_SESSION["password"] = $password; $_SESSION["username"] = $username; header("location: welkom.php"); exit; } else{ $password_err = "Het wachtwoord bestaat niet."; } } } else{ $username_err = "Geen account gevonden met deze gebruikersnaam."; } } else{ echo "Er is iets misgegaan, probeer het later opnieuw."; } } } } ?> <html> <head> <meta charset="UTF-8"> <title>Inloggen</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"> <style type="text/css"> body { font: 14px sans-serif; } .wrapper { width: 350px; padding: 20px; } </style> </head> <body> <center> <div class="wrapper"> <h2>Login</h2> <p>Voer uw gegevens in om door te gaan.</p> <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"> <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>"> <label>Gebruikersnaam</label> <input type="text" name="username" class="form-control" value="<?php echo $username; ?>"> <span class="help-block"><?php echo $username_err; ?></span> </div> <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>"> <label>Wachtwoord</label> <input type="password" name="password" class="form-control"> <span class="help-block"><?php echo $password_err; ?></span> </div> <div class="form-group"> <input id="button" type="submit" class="btn btn-primary" value="Verzenden"> <input id="button" type="reset" class="btn btn-default" value="Leeg maken"> </div> <p>Heb je nog geen account? <a href="register.php">Meld je hier aan</a>.</p> </form> </div> </center> </body> </html>
There are numerous issues with your code, but as for your problem, you are trying to process the login with an empty username and password which will never work.
So what should I change? And what other issues are there @benanamen?
The short answer, everything in the first 76 lines.
Take a look at this PDO login code and see what you can get out of it.
https://github.com/benanamen/perfect_app_oop/blob/master/public/login.php
There’s a lot that I would like to say, but I don’t think it’ll be appropriate to say. I’ll try to tell you in a manner that isn’t rude or anything. So basically, how I see this is that you should start from scratch. Save this as like an extra file to reference. Don’t use the entire code or anything. Just use it as a reference so that you know what fields you need and what not.
The first thing I would say is, separate your PHP
code from your HTML
code. That’s the biggest problem for you right now. You have PHP
code mished-mashed with HTML
and it’s just causing you so much problems. It may not be the current problem, but it does cause a little bit a headache to read what’s going on.
Next, you should change your logic because that’s also what’s causing the problem. I’ll help you out with just this part as a starting point. Then you have to figure it out yourself. What I am trying to doing is help guide you away from all of this mess and help you organize your code. I know that your code works for me and I know why that is, but what you need to do is take a step back and just breath. I’m going to use an example from my older threads.
So the first thing we need to really determine is what kind of action we are going to make. So we know we want a user to submit something, we are going to use 2 files. 1 for access point and 1 to create our visual stuff. We also need to create a connection file so that it’ll be easier to modify instead of going into 2 different files to modify the connection configurations.
db.php
<?php session_start(); // You may require this if you want to store sessions $db = mysqli_connect("localhost","id5386627_root","XXXxxx","id5386627_root"); if($db === false) { die("ERROR: Kon niet verbinden. " . mysqli_connect_error()); }
Once we have this file made, we’ll just only need to include this during the initial start of the files that require a database connection. This is more easier to work with than having these lines in 2 – 3 locations where you have to manually overwrite. It just saves time.
index.php
<?php require_once 'db.php'; if($_SERVER['REQUEST_METHOD'] == 'POST') { // Our logic here. } else { require_once 'our_html_file.php'; }
Now like I said, I’m not going to give you everything so I’m just going to start you off with this kind of layout. This layout will help you organize your HTML
stuff from your PHP
stuff. This just makes things so much neater and easier for your eyes.
our_html_file.php
<!DOCTYPE html> <head> <title>Untitled</title> </head> <body> </body> </html>
This is also another layout for you to use. You should start designing this layout first before you start working with the logic or data processing. Once you finish this, let me know. I’ll help you with the logic.
What I’m doing is helping you organize your work flow. Doing it this way will actually solve your problem once you are finished with the whole setup. Let me know when you are done. I know it’s not helping the current problem at hand, but that’s the problem. Just getting something to “work” can easily be done. What you should be doing is helping yourself in the long run.
Do you get any error messages when you try to login? Did one of them cause you to comment out your session_start()
line?
Slightly OT, but you can simplify this line
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
to read
<form method="post" action="">
if you just want the form submit to run the same script that drew it.
Here is an obvious problem, if you do have php logic and html in the same document, do the php first, before starting any html.
You can start sessions or have any headers after there has been any output to the browser.
Simply cutting and pasting the doctype to below the php block will fix this (to an extent). But in the longer term this is another good reason to stop mingling too much php with html and start separating them.
As long as we are going for simplicity, lets do it. Just remove the action altogether.
<form method="post">
I would argue that anything written in php is far from “perfect”. There is a huge learning gap for beginners in understanding several of the concepts exhibited in your repo. However, I understand what you are doing.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.
Bekijk hier meer informatie over dit onderwerp: php tabel maken