1 / 15

PHP MySQL TUTORIAL

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.

anais
Download Presentation

PHP MySQL TUTORIAL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP MySQL TUTORIAL SIMPLE LOGIN SYSTEM

  2. 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:

  3. 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");

  4. 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']); 

  5. 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!"); }

  6. The code • $_SESSION['loggedin'] = "YES";  • $_SESSION['name'] = $name;  • echo '<script type="text/javascript"> window.location = "index.php"</script>'; • die("You are now logged in!"); • }

  7. 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>"; ?>

  8. 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);

  9. 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:

  10. <?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>");  } ?>

  11. 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>");  }

  12. 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!

  13. Redirect • echo '<script type="text/javascript"> window.location = “secure_page.php"</script>'; • header('location:destinations.php?‘);

  14. 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>'; }

  15. <a href="logout.php?do=logout" title="Logout" onclick="return confirm('Are you sure you want to logout?')">Logout</a>

More Related