10 days Learn PHP Day 7

web front end seven thousand four hundred and ten 14 years ago (2011-04-08)

Learning purpose: learn how to use SESSION
SESSION has many functions, most of which are variable transfer between pages within the site. At the beginning of the page, we want session_start(); Open SESSION;
Then you can use the SESSION variable. For example, to assign a value:$_ SESSION['item']="item1"; To get the value, $item1=$_SESSION ['item '];, It's very simple. Here we may use some functions, for example, to determine whether a SESSION variable is empty. You can write: empty ($_SESSION ['inum ']) returns true or false.
Let's take a look at a login program to determine whether the user name and password are correct.
The login form is as follows: login.php

PHP code
  1. <table width= "100%"  height= "100%"  border= "0"  align= "center"  cellpadding= "0"  cellspacing= "0" >   
  2. <tr>   
  3. <form action= "checklogin.php"  method= "post" ><td align= "center"  valign= "middle" ><table width= "400"  border= "0"  cellpadding= "5"  cellspacing= "1"   class = "tablebg" >   
  4. <tr  class = "tdbg" >   
  5. <td colspan= "2" ><div align= "center" >Administrators Login</div></td>   
  6. </tr>   
  7. <tr  class = "tdbg" >   
  8. <td><div align= "center" >Username</div></td>   
  9. <td><div align= "center" >   
  10. <input name= "username"  type= "text"  id= "username" >   
  11. </div></td>   
  12. </tr>   
  13. <tr  class = "tdbg" >   
  14. <td><div align= "center" >Password</div></td>   
  15. <td><div align= "center" >   
  16. <input name= "password"  type= "password"  id= "password" >   
  17. </div></td>   
  18. </tr>   
  19. <tr  class = "tdbg" >   
  20. <td colspan= "2" ><div align= "center" >   
  21. <input type= "submit"  name= "Submit"  value= "Submit" >   
  22. <input type= "reset"  name= "Submit2"  value= "Clear" >   
  23. </div></td>   
  24. </tr>   
  25. </table></td></form>   
  26. </tr>   
  27. </table>  

This is how to process files

PHP code
  1. <?   
  2. require_once ( 'conn.php' );   
  3. session_start();   
  4. $username = $_POST [ 'username' ];   
  5. $password = $_POST [ 'password' ];   
  6. $exec = "select * from admin where username='" . $username . "'" ;   
  7. if ( $result =mysql_query( $exec ))   
  8. {   
  9.    if ( $rs =mysql_fetch_object( $result ))   
  10.   {   
  11.      if ( $rs ->password== $password )   
  12.     {   
  13.        $_SESSION [ 'adminname' ]= $username ;   
  14.       header( "location:index.php" );   
  15.     }   
  16.      else   
  17.     {   
  18.        echo   "<script>alert('Password Check Error!'); location.href='login.php';<\/ script>" ;   
  19.     }   
  20.   }   
  21.    else   
  22.   {   
  23.    echo   "<script>alert('Username Check Error!'); location.href='login.php';<\/ script>" ;   
  24.   }   
  25.   }   
  26. else   
  27. {   
  28. echo   "<script>alert('Database Connection Error!'); location.href='login.php';<\/ script>" ;   
  29. }   
  30. ?>  

Conn.php is as follows:

PHP code
  1. <?   
  2. $conn =mysql_connect ( "127.0.0.1" "" "" );   
  3. mysql_select_db( "shop" );    
  4. ?>  

Because $_SESSION ['adminname ']=$username; We can write a file to verify whether to log in as follows: checkadmin. ASP

PHP code
  1. <?   
  2. session_start();   
  3. if ( $_SESSION [ 'adminname' ]== '' )   
  4. {   
  5. echo   "<script>alert('Please Login First'); location.href='login.php';<\/ script>" ;   
  6. }   
  7. ?>  

Hehe, let's talk about how to make a page today and tomorrow.