Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

04 Html Form Get Post Login System

3,094 views

Published on

HTML form handing with PHP and POST varibales

Published in: Technology
  • Be the first to comment

04 Html Form Get Post Login System

  1. 1. PHP Day 4 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com http://www.php.net
  2. 2. HTML Forms <ul><li><form name=“form1” method=“ post ” action=“process_form.php”> …. </form> </li></ul><ul><li><input type=“text” name=“user_name”> </li></ul><ul><li><input type=&quot;password&quot; name=&quot;pass_word“> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;submit_form&quot; value=&quot;Submit&quot; /> </li></ul><ul><li>For display formatted forms use tables. </li></ul><ul><li>For more see http://www.w3schools.com/html/html_forms.asp </li></ul>
  3. 3. Require and Include <ul><li>require(‘path/to/file_name.php’) </li></ul><ul><li>include(‘path/to/file_name.php’) </li></ul><ul><ul><li>Difference is require will throw a Fatal error is file required is not found include will just throw a warning. </li></ul></ul><ul><li>require_once(‘path/to/file_name.php’); </li></ul><ul><li>include_once(‘path/to/file_name.php’); </li></ul><ul><ul><li>To require or include a file just once. </li></ul></ul>
  4. 4. Session Management <ul><li>session_start — Initialize session data </li></ul><ul><li>$_SESSION[‘index’] = “value”; </li></ul><ul><ul><li>$_SESSION[‘user_name’] = $user_name; </li></ul></ul><ul><li>session_destroy — Destroys all data registered to a session </li></ul>
  5. 5. A simple login interface
  6. 6. Login Check function <ul><li>Getting Variables and starting session. </li></ul><ul><li><?php </li></ul><ul><li>session_start(); </li></ul><ul><li>$user_name = $_POST[‘user_name’]; </li></ul><ul><li>$pass_word = $_POST[‘pass_word’]; </li></ul>
  7. 7. Login Check function <ul><li>The Function definition </li></ul><ul><li>function check_login($user_name, $pass_word){ </li></ul><ul><li>if(($user_name==&quot;admin&quot;) && ($pass_word === &quot;test&quot;)) </li></ul><ul><li>{ </li></ul><ul><li>//set session </li></ul><ul><li>$_SESSION['user_name'] = $user_name; </li></ul><ul><li>$_SESSION['user_logged_in'] = true; </li></ul><ul><li>return 1; </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  8. 8. Login check function <ul><li>Function call and process </li></ul><ul><li>$user_ok = check_login($user_name, $pass_word); //called from check_login_function.php </li></ul><ul><li>if($user_ok){ </li></ul><ul><li>header(&quot;Location: logged_in.php&quot;); /* Redirect browser */ </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>header(&quot;Location: not_logged_in.php&quot;); </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  9. 9. Either you login or are not logged in
  10. 10. Session to authenticate users <ul><li>Address security issues. </li></ul><ul><li>Put content that should be seen by logged in users or even by user type. </li></ul><ul><li>User module and session management is essential to almost every system. </li></ul>
  11. 11. Questions
  12. 12. Let’s start practice <ul><li>Build your own login system with upto 3 user name and password combination with user of array, refer to code of Day04 . </li></ul><ul><li>Among the 3 one should be of type admin and remaining two of type user. </li></ul><ul><li>The logged in page should not be accessible directly after typing full URL/address in the address bar. </li></ul>

×