170 likes | 362 Views
PHP MySQL TUTORIAL. SIMPLE LOGIN SYSTEM. Introduction. Login is a very important part of any website, from securing contents from prying eyes to tracking visitors on your website. This tutorial is a simple login form for a website.
E N D
PHP MySQL TUTORIAL SIMPLE LOGIN SYSTEM
Introduction • Login is a very important part of any website, from securing contents from prying eyes to tracking visitors on your website. • This tutorial is a simple login form for a website. • Firstly, make a new file called "login.php" and start off by writing this code:
The code • <?php • session_start(); • mysql_connect(“localhost","PUT YOUR DATABASE USER HERE","PUT YOUR DATABASE PASSWORD HERE"); • mysql_select_db("PUT YOUR DATABASE HERE");
The code • if(isset($_SESSION['loggedin'])){die("You are already logged in!");} • if(isset($_POST['submit'])){ $name = mysql_real_escape_string($_POST['username']); • $pass = mysql_real_escape_string($_POST['password']);
The code • $mysql = mysql_query("SELECT * FROM users WHERE name = '$name' AND password = '$pass'"); • if(mysql_num_rows($mysql) < 1) { die("Password was probably incorrect!"); }
The code • $_SESSION['loggedin'] = "YES"; • $_SESSION['name'] = $name; • echo '<script type="text/javascript"> window.location = "index.php"</script>'; • die("You are now logged in!"); • }
The code echo "<form type='login.php' method='POST'>Username: <br><input type='text' name='username'><br>Password: <br><input type='password' name='password'> <br><input type='submit' name='submit' value='Login'></form>"; ?>
Database • After you have put that code, run this query on your database: CREATE TABLE `users` (`id` BIGINT( 60 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,`name` VARCHAR( 50 ) NOT NULL ,`password` VARCHAR( 32 ) NOT NULL ,`date` INT( 32 ) NOT NULL ,`email` VARCHAR( 80 ) NOT NULL);
Next • And then add all of the users you wish to the table! • To integrate this within your website, you can do the following at the top of your site:
<?phpsession_start(); // NEVER forget this!if(!isset($_SESSION['loggedin'])){die("To access this page, you need to <a href='login.php'>LOGIN</a>"); <?phpsession_start(); // NEVER forget this!if(!isset($_SESSION['loggedin'])){die("To access this page, you need to <a href='login.php'>LOGIN</a>"); } ?>
list_records.php <?php session_start(); // NEVER forget this!if(!isset($_SESSION['loggedin'])){die("To access this page, you need to <a href='login.php'>LOGIN</a>"); }
And to say like "Hello there, {USERNAME}" you would put this snippet anywhere you want it to appear: <?php echo " Hello there, ".$_SESSION['name'] ;?> • Remember, DO NOT FORGET "session_start();" at the top of every page in which you want the user to log in or be logged in to access!
Redirect • echo '<script type="text/javascript"> window.location = “secure_page.php"</script>'; • header('location:destinations.php?‘);
logout.php <?php session_start(); if($_GET['do'] == "logout") { unset($_SESSION['loggedin']); unset($_SESSION['name']); session_destroy(); echo '<script type="text/javascript"> window.location = "login.php"</script>'; }
<a href="logout.php?do=logout" title="Logout" onclick="return confirm('Are you sure you want to logout?')">Logout</a>