Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
[PHP] Login & Logout
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Obey" data-source="post: 365873" data-attributes="member: 40005"><p><strong>This is the first time I've ever used PHP so forgive me for any errors.</strong></p><p>I gathered a simple login and logout script that DOES need access to your database. I've included a db.php file below so if you don't already have one, make one and include the code below inside. There is only HTML in the logout script but it's easy to make a text/password field in the login one.</p><p><strong>Includes:</strong></p><ul> <li data-xf-list-type="ul">SQL injection protection (unintentional rhyming)</li> <li data-xf-list-type="ul">Checks if all fields are filled in and displays an error if they aren't.</li> <li data-xf-list-type="ul">Automatically directs to index.php when logged in, change it if you want it to direct somewhere else.</li> </ul><p>I mean it is only a login and logout script, it is really simple and there's nothing special.</p><p></p><p>Login:</p><p>[code=php]</p><p><?php</p><p> session_start();</p><p></p><p> if(isset($_POST['login'])) {</p><p> include_once("db.php");</p><p> $username = strip_tags($_POST['username']);</p><p> $password = strip_tags($_POST['password']);</p><p> $error = '';</p><p> if( empty( $username ) )</p><p> {</p><p> $error .= 'You have to enter your username!<br>';</p><p> }</p><p> if( empty( $password ) )</p><p> {</p><p> $error .= 'You have to enter your password!<br>';</p><p> }</p><p> if( $error )</p><p> {</p><p> echo "$error";</p><p> $disablebutton = false;</p><p> }</p><p> else</p><p> </p><p> </p><p> </p><p> $username = stripslashes($username);</p><p> $password = stripslashes($password);</p><p> </p><p> $username = mysqli_real_escape_string($db, $username);</p><p> $password = mysqli_real_escape_string($db, $password);</p><p></p><p> $password = md5($password);</p><p></p><p> $sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";</p><p> $query = mysqli_query($db, $sql);</p><p> $row = mysqli_fetch_array($query);</p><p> $id = $row['id'];</p><p> $db_password = $row['password'];</p><p></p><p> if($password == $db_password) {</p><p> $_SESSION['username'] = $username;</p><p> $_SESSION['id'] = $id;</p><p> header("Location: index.php");</p><p> } else {</p><p> echo "You didn't enter the correct details!";</p><p> }</p><p></p><p> }</p><p>?></p><p>[/code]</p><p>Logout:</p><p>[code=php]</p><p><?php</p><p> session_start();</p><p> session_destroy();</p><p> </p><p>?></p><p></p><p><html></p><p><head></p><p><meta http-equiv="refresh" content="1;url=login.php"></p><p><title>Logout</title></p><p></head></p><p></html></p><p>[/code]</p><p>Db.php:</p><p>[code=php]</p><p><?php</p><p>$db = new mysqli('localhost', 'root', 'password', 'db_name');</p><p></p><p>if ($db->connect_errno > 0) {</p><p> die('Unable to connect to the database [' . $db->connect_error . ']');</p><p>}</p><p>?></p><p>[/code]</p><p>The checking if you filled in all fields code I got from [user=1872]@Markshall[/user] so thank you to him.</p><p>This was aimed at people like me who want to use one of these but doesn't know how; I'm sure it will help someone.</p></blockquote><p></p>
[QUOTE="Obey, post: 365873, member: 40005"] [B]This is the first time I've ever used PHP so forgive me for any errors.[/B] I gathered a simple login and logout script that DOES need access to your database. I've included a db.php file below so if you don't already have one, make one and include the code below inside. There is only HTML in the logout script but it's easy to make a text/password field in the login one. [B]Includes:[/B] [LIST] [*]SQL injection protection (unintentional rhyming) [*]Checks if all fields are filled in and displays an error if they aren't. [*]Automatically directs to index.php when logged in, change it if you want it to direct somewhere else. [/LIST] I mean it is only a login and logout script, it is really simple and there's nothing special. Login: [code=php] <?php session_start(); if(isset($_POST['login'])) { include_once("db.php"); $username = strip_tags($_POST['username']); $password = strip_tags($_POST['password']); $error = ''; if( empty( $username ) ) { $error .= 'You have to enter your username!<br>'; } if( empty( $password ) ) { $error .= 'You have to enter your password!<br>'; } if( $error ) { echo "$error"; $disablebutton = false; } else $username = stripslashes($username); $password = stripslashes($password); $username = mysqli_real_escape_string($db, $username); $password = mysqli_real_escape_string($db, $password); $password = md5($password); $sql = "SELECT * FROM users WHERE username='$username' LIMIT 1"; $query = mysqli_query($db, $sql); $row = mysqli_fetch_array($query); $id = $row['id']; $db_password = $row['password']; if($password == $db_password) { $_SESSION['username'] = $username; $_SESSION['id'] = $id; header("Location: index.php"); } else { echo "You didn't enter the correct details!"; } } ?> [/code] Logout: [code=php] <?php session_start(); session_destroy(); ?> <html> <head> <meta http-equiv="refresh" content="1;url=login.php"> <title>Logout</title> </head> </html> [/code] Db.php: [code=php] <?php $db = new mysqli('localhost', 'root', 'password', 'db_name'); if ($db->connect_errno > 0) { die('Unable to connect to the database [' . $db->connect_error . ']'); } ?> [/code] The checking if you filled in all fields code I got from [user=1872]@Markshall[/user] so thank you to him. This was aimed at people like me who want to use one of these but doesn't know how; I'm sure it will help someone. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[PHP] Login & Logout
Top