Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this is for archive purposes
- //index.php
- <?php
- session_start();
- if (isset($_GET["logout"])) { // means the user clicked the logout button somewhere across the site
- session_unset(); // unset the session array i.e. destroy the data and the array
- session_destroy(); // terminate the session.
- }
- if (isset($_SESSION['user'])){
- include('member.php');
- }
- else {
- include('guest.php');
- }
- ?>
- <html>
- <head>
- <title>My first login session</title>
- <link rel="stylesheet" type="text/css" href="main.css">
- </head>
- <body>
- <div class="container">
- <div class="nav">Navigation will go here</div>
- <div class="content">
- <h2>Let's learn about login!</h2>
- <p>Go to <a href="login.php">next page</a></p>
- </div>
- <div class="footer"> Footer will go here</div>
- </div>
- </body>
- </html>
- //login.php
- <?php
- session_start();
- if (isset($_SESSION['user'])){
- include('member.php');
- }
- else {
- include('guest.php');
- }
- ?>
- <html>
- <head>
- <title>Still Learning Login</title>
- <link rel="stylesheet" type="text/css" href="main.css">
- </head>
- <body>
- <div class="container">
- <div class="nav">Navigation will go here</div>
- <div class="content">
- <h2>Let's keep learning about login sessions!</h2>
- <p>Look at what happens if I initialize the user session here...<br /></p>
- <form id="login" name="login" method="post" action="verify_login.php">
- <fieldset>
- <legend>Login information:</legend>
- Username:
- <br>
- <input type="text" id="userID" name="userID">
- <br>
- Password:
- <br>
- <input type="text" id="password" name="password">
- </fieldset>
- <input type="submit" name="submit" value="Login">
- </form>
- <p>Go to <a href="verify_login.php">page 3.</a></p>
- </div>
- <div class="footer"> Footer will go here</div>
- </div>
- </body>
- </html>
- //verify_login.php
- <?php
- session_start();
- $message = "";
- if (isset($_SESSION['user'])){
- include('member.php');
- }
- else {
- include('guest.php');
- }
- ?>
- <html>
- <head>
- <title>Still testing login here</title>
- <link rel="stylesheet" type="text/css" href="main.css">
- </head>
- <body>
- <div class="container">
- <div class="nav">Navigation will go here</div>
- <div class="content">
- <?php echo $message . "<br>"; ?>
- <h2>Let's keep learning about login sessions! just a little more...</h2>
- <p>Let's see who we have logged in.<br /></p>
- <?php
- if ($_SERVER["REQUEST_METHOD"] == "POST"){
- $userID = $_POST["userID"];
- $password = $_POST["password"];
- $fname = "";
- //store our connection credentials in variables;
- $server = '';
- $user = '';
- $pass = '';
- //there is no password for the my current setup of MySQL
- $dbase = '';
- //make the actual connection to myaql and the chosen database
- $conn = mysqli_connect($server, $user, $pass, $dbase);
- //if the connection failed print error message
- if (!$conn) {
- die('Could not connect to database because: ' . mysqli_connect_error());
- }
- else echo "You are successfully connected to $dbase <br>";
- //Create the query string
- $query = "select * from fans where userID = '$userID'";
- //execute the query
- $result = mysqli_query($conn, $query);
- if ($result) {
- if (mysqli_num_rows($result) > 0) {
- // if we are in this block it means that $result was NOT false and it contained
- // at least one row of data... so let's process the data one row at a time
- while ($row = mysqli_fetch_assoc($result)) {
- // if we are in this inner block it means that we just successfully retrieved
- // a row of data from the resultset held in $result. We can embed this in
- // our HTML page as shown below
- $db_userID = $row["userID"];
- $db_password = $row["password"];
- $db_fname = $row["fname"];
- }
- //end while loop
- }
- //end if 1 or more rows
- else
- echo "<br>No rows returned";
- }
- mysqli_close($conn);
- if($password != $db_password) {
- echo "Incorrect password entered, please <a href='login.php'>login</a> again.";
- } else {
- $_SESSION['user'] = $userID;
- $_SESSION['user_fname'] = $db_fname;
- echo "Our current user is ".$_SESSION['user']. ".";
- }
- }
- ?>
- <p>Go back to <a href="index.php">Home page.</a></p>
- </div>
- <div class="footer"> Footer will go here</div>
- </div>
- </body>
- </html>
- //guest.php
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>Guest</title>
- <link rel="stylesheet" type="text/css" href="main.css">
- </head>
- <body>
- <div class="container">
- <div class="content">
- <p>If you have not logged in as yet, you can login <a href="login.php">here</a>.</p>
- </div>
- </div>
- </body>
- </html>
- //member.php
- <?php
- $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>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement