14-session_variables/index.php

Go to the documentation of this file.
00001 <?php
00011 //When using sessions we must start session as early in program
00012 //as possible, preferably first line.
00013 session_start();
00014 
00017 session_register('logged_in');
00018 
00019 
00020 //including file which has common functions like redirect
00021 //and get_session which will be reused in many scripts
00022 require('common_functions.php');
00023 
00024 
00027 $message='';
00028 
00029 
00039 function get_post($var1)
00040 {
00041     if(isset($_POST[$var1]))
00042         return $_POST[$var1];
00043     else
00044         return "";
00045 }
00046 
00047 
00048 
00055 function show_login_page()
00056 {
00057     global $message;
00058     ?>
00059         <html>
00060             <head>
00061                 <title>Login page</title>
00062             </head>
00063             <body>
00064                 <?php echo $message; ?>
00065                 <form action="index.php" method="POST">
00066                     Username: <input type="text" name="username1" value="" /> <br/>
00067                     Password: <input type="password" name="password1" value="" /> <br/>
00068                     <input type="submit" value="Login" name="submit1" /><br/>
00069                 </form>
00070             </body>
00071         </html>
00072     <?php
00073 }
00074 
00075 
00088 function main()
00089 {
00090     global $message;
00091 
00092     //Get value of session variable with name 'logged_in'
00093     $username1=get_session('logged_in');
00094 
00095     //If user has already logged in this value should not
00096     //be null. In that case redirect user to welcome page.
00097     if($username1!="")
00098         redirect('welcome.php');
00099     else if(get_post('submit1')=="")
00100     {   
00101         //if page is being displayed for first time
00102         //show login page without doing anything to session variables
00103         show_login_page();
00104     }
00105     else
00106     {   
00107         //if page is being submitted, check if username 
00108         //and password match
00109         $username1=get_post('username1');
00110         $password1=get_post('password1');
00111 
00112         if($username1==$password1 && $username1!="")
00113         {
00114             //if username and passwod match, then redirect
00115             //to welcome page after setting proper value of
00116             //session variable 'logged_in'
00117             $_SESSION['logged_in']=$username1;
00118             redirect('welcome.php');
00119         }
00120         else
00121         {   
00122             //If username and password do not match, show login
00123             //page again with error message.
00124             //Note that $message must be declared as global in main()
00125             //for this to work.
00126             $message="Sorry! Username and password do not match.<br/>\n";
00127             show_login_page();
00128         }
00129     }
00130 }
00131 
00132 
00133 main();
00134 
00135 ?>

Generated on Fri Nov 4 14:16:54 2011 for PHP example documentation by  doxygen 1.4.7