Simple PHP PDO login form

Simple PHP PDO login form

HydroTutorials

9 лет назад

22,043 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Abdias Noel
Abdias Noel - 13.10.2020 19:05

The user's input is not sanitized and it is used in session variable...
But after all, it's a good start

Ответить
Juan Rey
Juan Rey - 06.04.2019 00:07

Hey, for the index.php altough im setting the html exactly as you, i just keep getting a blank page, any ideas?

Ответить
Anh Vuong
Anh Vuong - 10.07.2018 20:25

thank you so much

Ответить
David Ashby
David Ashby - 31.07.2017 17:00

I have just come back to this and everything bar 1 thing works. If I put the url in for the login.php page I can access it. I shouldn't be able to acess it unless I have logged in, how can I ensure this is the case?

Ответить
Morialan Oliveira
Morialan Oliveira - 28.05.2017 07:15

Hey, man!
Thanks for this work.
I'm from brazil, and this tutorial helped me a lot to make a job for university.
Thanks one more time!

Ответить
Imane Nady
Imane Nady - 25.02.2017 03:13

i tried adding the php code to my website, but when i enter values in the form and click on login, the page where the session is started doesn't show, the login page stays the same

Ответить
Natalie
Natalie - 26.09.2016 00:09

If you go directly to the login page, what happens ? Does this not make the login thing completely useless ?

Ответить
Kevin Ike
Kevin Ike - 22.07.2016 15:39

hooi!

ik krijg deze foutmelding wat moet ik doen?
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FORM gebruikers WHERE Gebruikersnaam = kevin AND Wachtwoord = 123' at line 1' in C:..\wwwroot\inc\inloggen.php:26 Stack trace: #0 C:..\wwwroot\inc\inloggen.php(26): PDOStatement->execute() #1 C:..\wwwroot\inloggen.php(57): include('C:\\domains\\vane...') #2 {main} thrown in C:..\wwwroot\inc\inloggen.php on line 26

code:

<?php
session_start();
$servername = "localhost";
$username = "...";
$password = "...";

try {
$conn = new PDO("mysql:host=$servername;dbname=...", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
catch(PDOException $e)
{
echo "ERROR: " . $e->getMessage();
echo "<br>";
echo "Geen goede database connectie. Herlaad deze pagina.";
}

if (isset($_POST['inloggen'])) {

$gebruikersnaam = $_POST['gnaam'];
$wachtwoord = $_POST['wwoord'];

$query = $conn->prepare("SELECT COUNT ID FORM ... WHERE Gebruikersnaam = $gebruikersnaam AND Wachtwoord = $wachtwoord");
$query->execute();
$count = $query->fetchColumn();
if ($count == "1"){
$_SESSION['gebruikersnaam'] = $gebruikersnaam;

header ('location: ingelogd/index.php');
}
}

?>

Ответить
Xkull iber
Xkull iber - 30.05.2016 12:11

Thank you :)

Ответить
LyonOconner
LyonOconner - 04.05.2016 19:23

Fatal error: Call to undefined method PDO::execute() in C:\xamp\htdocs\Webphp\index.php on line 12

Ответить
Luke Grice
Luke Grice - 16.04.2016 23:24

<?php
session_start();

$conn = new PDO('mysql:host=localhost;dbname=logintutorial', 'root', '');

if(isset($_POST['login'])){
$username=$_POST['username'];
$password=$_POST['password'];

$query = $conn->prepare("SELECT COUNT('id') FROM 'users' WHERE 'username' = '$username' AND 'password' = '$password'");
$query = $conn->execute();

$count = $query->fetchColumn();

if($count == "1"){
$_SESSION['username'] = $username;

header('location: login.php');
}
}
?>

Ответить
Unseptium Anonymous
Unseptium Anonymous - 01.04.2016 13:16

Hi there, I tried making the same thing without a password, but it won't log in, even though I have edited the code so that it would work (or at least I think so)
Here's the code, can you tell me if I did something wrong here, and if so, how I can fix it?

<?php
session_start();

$conn = new PDO('mysql:host=localhost;dbname=t3inf', 'root', '');

if(isset($_Post['login'])){
$username = $_post['username'];

$query = $conn->prepare("SELECT COUNT('id') FROM 'users' WHERE 'username' = '$username'");
$query->execute();

//Kind regards.

Ответить
looksclasswhatsthat
looksclasswhatsthat - 30.03.2016 16:35

I have got this to work but what i want to do is have two diffrent login scanerios depending on who logs in i was trying to do this with the if count = 1 load a certain page else if count = 2 load a diffrent page is thsi possible thanks

Ответить
David Ashby
David Ashby - 28.03.2016 16:01

Nice tutorial, I have been looking for this for a while. Thanks

Ответить
nguyen kings
nguyen kings - 22.03.2016 05:29

Error -->Try : $query = $conn->prepare("SELECT COUNT(id) FROM users WHERE username = '$username' AND password = '$password'");

Ответить
C S
C S - 17.03.2016 20:48

hi, i swear I'm doing this right but my login page keeps coming up blank, i don't think the index page is connecting properly to the login page, if you could help me out would really appreciate it

Ответить
Jonathan Serafin
Jonathan Serafin - 10.03.2016 21:31

I'm doing the same thing than you and it doesn't works

Ответить
per hellborg
per hellborg - 11.02.2016 22:03

Nothing happens when i try to login. The textbox just get empty when i submit :( please help

Ответить
Testificow
Testificow - 02.11.2015 21:57

Thanks for uploading this, do you also know how to hash the passwords so, you can't see them in the database?

Ответить
Ron Curington
Ron Curington - 29.10.2015 21:33

My favorite videos are the ones that have sound.

Ответить
HydroTutorials
HydroTutorials - 15.10.2015 23:24

Michiko;

Your session is not started.

put an isset around it,

if (isset($_SESSION['username']{
echo "Welkom" . $_SESSION['username'];
}
this will prevent showing errors.

Also check your login script to make sure the username session is set.

$_SESSION['username] = true must be set.

You can check by using the var_dump function like

Var_dump($_SESSION['username']);

Ответить
Michiko Niña Elnar
Michiko Niña Elnar - 13.10.2015 23:01

<?php
session_start();

echo "Welcome" . $_SESSION['username'];

if(isset($_POST['logout'])){
session_start();
session_destroy();

header('location: index.php');
}



?>

<html>
<head>
<title>Logged in</title>
</head>

<body>
<form method = "post" name = "logout">
<input type = "submit" name="logout" value="log me out">
</form>
</body>
</html>

Notice: Undefined index: username in C:\wamp\www\Work_it\login.php on line 4

i followed your tutorial but after I ran the program it gave an error T__T help please :D Thank you so much

Ответить
Samuel Landeza
Samuel Landeza - 08.10.2015 02:58

thanks...

Ответить
apokalum
apokalum - 03.09.2015 18:10

How would you make this a registration form?

Ответить
Roman Ab
Roman Ab - 26.08.2015 20:29

security ... security ...

Ответить
diegoigr7
diegoigr7 - 07.07.2015 04:57

thanks!!! best regards from chile :)

Ответить
Rehab Kharbach
Rehab Kharbach - 26.05.2015 01:29

can you please send me the program used by email : 
[email protected] 
i'll be very greatful

Ответить
RickRulezz
RickRulezz - 11.05.2015 18:26

good video this is what i needed for a project because i did follow a tutorial where he makes a login system for a cms but that thing didn't work so this will work


btw hoi land genoot

Ответить