+ 2

How can create a login in PHP?

How can create a basic login in PHP? For my web project, i'm new in PHP.

16th Feb 2018, 3:09 PM
Connor Hunter
Connor Hunter - avatar
4 Answers
+ 1
Hi by php and a little Html. step1:create a 'login.html' file that send username & password by submit bottom step2: set its action to 'login.php' and "POST" method then create 'login.php" and get the inputs from "Session" . step3: varify username and password with your own (also can create a "config.php" file that conect to a database then search and check them in users table ,don't forget to include config.php in other files.) step4:after varify redirect to "home.html" or "login.html" again. thats all.
16th Feb 2018, 5:39 PM
Amirhosein Morteza
Amirhosein Morteza - avatar
+ 3
For a real basic login then use something like the below. But really you should look into creating a full login system with sessions etc. PLACE THIS AT TOP OF DOCUMENT <?php $username = "admin"; $password = "admin"; $nonsense = "ds89fe9e32hjsw0a82w"; if (isset($_COOKIE['PrivatePageLogin'])) { if ($_COOKIE['PrivatePageLogin'] == md5($password.$nonsense)) { ?> PLACE THIS AT BOTTOM OF DOCUMENT <?php exit;} else {echo "Bad Cookie.";exit;}} if (isset($_GET['p']) && $_GET['p'] == "login") { if ($_POST['user'] != $username) { echo "Sorry, that username does not match."; exit; } else if ($_POST['keypass'] != $password) { echo "Sorry, that password does not match."; exit; } else if ($_POST['user'] == $username && $_POST['keypass'] == $password) { setcookie('PrivatePageLogin', md5($_POST['keypass'].$nonsense)); header("Location: $_SERVER[PHP_SELF]"); } else { echo "Sorry, you could not be logged in at this time. You may need to quit your browser session to kill the cookie previously set and then login again."; } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post" style="width:250px;margin: 50px auto 0 auto;background-color:#ffffff;padding:15px;-moz-border-radius: 4px;-webkit-border-radius: 4px;border-radius: 4px;-khtml-border-radius: 4px;"><h1 style="font-family: arial; text-align: center;">LED Flex Products </h1> <input type="text" name="user" id="user" placeholder="Username" style="padding: 7px;border: #cccccc 1px solid;border-radius:4px;float:left;width: 100%;margin-bottom: 10px;"/> <input type="password" name="keypass" id="keypass" placeholder="Password" style="padding: 7px;border: #cccccc 1px solid;border-radius:4px;float:left;width: 100%;margin-bottom: 10px;"/> <input type="submit" id="submit" class="button_select" value="Login" style="text-decoration: none; background-color:#111111;padding:12px;width:100%;border:solid 1px #111111;color:#ffffff;font-size:14px;transition: 0.3s;"/>
16th Feb 2018, 3:34 PM
ihateonions
+ 1
Salted md5 passwords are not a good idea. Use sha256 instead. http://php.net/manual/en/function.hash.php. You shouldn't store passwords in cookies even if they are hashed.
16th Feb 2018, 4:03 PM
Toni Isotalo
Toni Isotalo - avatar
0
What kind of login are you talking about. Is there a registration as well?
16th Feb 2018, 4:05 PM
Toni Isotalo
Toni Isotalo - avatar