Advertisement
thotfrnk

week12.php

Nov 26th, 2023 (edited)
1,060
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.11 KB | None | 0 1
  1. //this is for archive purposes
  2.  
  3. //index.php
  4.  
  5. <?php
  6.     session_start();
  7.  
  8.     if (isset($_GET["logout"])) { // means the user clicked the logout button somewhere across the site
  9.         session_unset(); // unset the session array i.e. destroy the data and the array
  10.         session_destroy(); // terminate the session.
  11.     }
  12.  
  13.     if (isset($_SESSION['user'])){
  14.         include('member.php');
  15.     }
  16.     else {     
  17.         include('guest.php');
  18.     }
  19.            
  20. ?>
  21. <html>
  22.     <head>
  23.         <title>My first login session</title>  
  24.         <link rel="stylesheet" type="text/css" href="main.css">
  25.     </head>
  26.     <body>
  27.         <div class="container">
  28.  
  29.             <div class="nav">Navigation will go here</div>
  30.      
  31.  
  32.             <div class="content">
  33.                 <h2>Let's learn about login!</h2>
  34.            
  35.                 <p>Go to <a href="login.php">next page</a></p>
  36.  
  37.             </div>
  38.      
  39.      
  40.             <div class="footer"> Footer will go here</div>
  41.  
  42.  
  43.            
  44.         </div>
  45.        
  46.     </body>
  47. </html>
  48.  
  49. //login.php
  50.  
  51. <?php
  52.     session_start();
  53.     if (isset($_SESSION['user'])){
  54.         include('member.php');
  55.        
  56.     }
  57.     else {
  58.        
  59.         include('guest.php');
  60.     }
  61.            
  62. ?>
  63. <html>
  64.     <head>
  65.         <title>Still Learning Login</title>
  66.         <link rel="stylesheet" type="text/css" href="main.css">    
  67.     </head>
  68.     <body>
  69.         <div class="container">
  70.  
  71.             <div class="nav">Navigation will go here</div>
  72.      
  73.  
  74.             <div class="content">
  75.  
  76.                 <h2>Let's keep learning about login sessions!</h2>
  77.                     <p>Look at what happens if I initialize the user session here...<br /></p>
  78.  
  79.                     <form id="login" name="login" method="post" action="verify_login.php">
  80.                         <fieldset>
  81.                             <legend>Login information:</legend>
  82.  
  83.                             Username:
  84.                             <br>
  85.                             <input type="text" id="userID" name="userID">
  86.  
  87.                             <br>
  88.  
  89.                             Password:
  90.                             <br>
  91.                             <input type="text" id="password" name="password">
  92.                         </fieldset>
  93.  
  94.                         <input type="submit" name="submit" value="Login">
  95.                     </form>
  96.  
  97.                 <p>Go to <a href="verify_login.php">page 3.</a></p>
  98.             </div>
  99.      
  100.      
  101.             <div class="footer"> Footer will go here</div>
  102.  
  103.  
  104.         </div>
  105.        
  106.     </body>
  107. </html>
  108.  
  109. //verify_login.php
  110.  
  111. <?php
  112.     session_start();
  113.     $message = "";
  114.     if (isset($_SESSION['user'])){
  115.         include('member.php');
  116.        
  117.     }
  118.     else {
  119.        
  120.         include('guest.php');
  121.     }
  122.            
  123. ?>
  124. <html>
  125.     <head>
  126.         <title>Still testing login here</title>
  127.         <link rel="stylesheet" type="text/css" href="main.css">
  128.     </head>
  129.     <body>
  130.         <div class="container">
  131.  
  132.             <div class="nav">Navigation will go here</div>
  133.      
  134.  
  135.             <div class="content">
  136.                 <?php echo $message . "<br>"; ?>   
  137.  
  138.                 <h2>Let's keep learning about login sessions! just a little more...</h2>
  139.                 <p>Let's see who we have logged in.<br /></p>
  140.  
  141.         <?php
  142.                 if ($_SERVER["REQUEST_METHOD"] == "POST"){
  143.                 $userID = $_POST["userID"];
  144.                 $password = $_POST["password"];
  145.                 $fname = "";
  146.  
  147.                 //store our connection credentials in variables;  
  148. $server = '';  
  149. $user = '';  
  150. $pass = '';
  151. //there is no password for the my current setup of MySQL  
  152. $dbase = '';      
  153. //make the actual connection to myaql and the chosen database  
  154. $conn = mysqli_connect($server, $user, $pass, $dbase);  
  155.  //if the connection failed print error message
  156.  if (!$conn) {    
  157.     die('Could not connect to database because: ' . mysqli_connect_error());    
  158.  }  
  159.   else echo "You are successfully connected to $dbase <br>";
  160.  
  161.     //Create the query string
  162.     $query = "select * from fans where userID = '$userID'";
  163.  
  164.     //execute the query
  165.     $result = mysqli_query($conn, $query);
  166.     if ($result) {
  167.  
  168.         if (mysqli_num_rows($result) > 0) {
  169.  
  170.             // if we are in this block it means that $result was NOT false and it contained
  171.             // at least one row of data... so let's process the data one row at a time
  172.  
  173.             while ($row = mysqli_fetch_assoc($result)) {
  174.  
  175.                 // if we are in this inner block it means that we just successfully retrieved
  176.  
  177.                 // a row of data from the resultset held in $result. We can embed this in
  178.                 // our HTML page as shown below
  179.  
  180.                 $db_userID = $row["userID"];
  181.                 $db_password = $row["password"];
  182.                 $db_fname = $row["fname"];
  183.  
  184.             }
  185.             //end while loop
  186.  
  187.         }
  188.         //end if 1 or more rows
  189.  
  190.         else
  191.             echo "<br>No rows returned";
  192.     }
  193.     mysqli_close($conn);
  194.  
  195.     if($password != $db_password) {
  196. echo "Incorrect password entered, please <a href='login.php'>login</a> again.";
  197.     } else {
  198.         $_SESSION['user'] = $userID;
  199.         $_SESSION['user_fname'] = $db_fname;   
  200.         echo "Our current user is ".$_SESSION['user']. ".";
  201.     }
  202.  
  203. }
  204.                 ?>
  205.  
  206.  
  207.             <p>Go back to  <a href="index.php">Home page.</a></p>
  208.         </div>
  209.        
  210.             <div class="footer"> Footer will go here</div>
  211.  
  212.             </div>
  213.  
  214.      
  215. </body>
  216. </html>
  217.  
  218. //guest.php
  219.  
  220. <!DOCTYPE html>
  221. <html lang="en">
  222.     <head>
  223.         <title>Guest</title>
  224.         <link rel="stylesheet" type="text/css" href="main.css">    
  225.     </head>
  226.     <body>
  227.         <div class="container">
  228.      
  229.  
  230.             <div class="content">
  231.                    
  232.       <p>If you have not logged in as yet, you can login <a href="login.php">here</a>.</p>
  233.  
  234. </div>
  235.  
  236. </div>
  237. </body>
  238. </html>
  239.  
  240. //member.php
  241.  
  242. <?php
  243.  
  244. $message = "<p>$_SESSION[user_fname] is logged in.</p><form action='index.php' method='get'><input type=submit value='logout' name='logout' id='logout' /></form>";
  245.  
  246. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement