• 03Jul

    This tutorial goes over implementing a log in system into the basic blog from tutorial 10/11. The goal is to use a session, a form, and a few pages to log in, log out, and of course, setting up future tools for the blog administration that will be shown in future tutorials.
    You may refer to Tutorial 8(Login Concept) as we go though this tutorial.

    Part 1:

    Make sure to click on Read more so that you may see Part 2 and the sources(including downloads)!

    Part 2:

    So first of all, Here is my list of goals

    • Create a login page
    • Edit the existing blog.php to prove I am logged in
    • Create a way to log out
    • Add a place for future posting

    Additionally, to see how the blog was set up initially, and what has been added to it, please see Tutorial 10 and Tutorial 11.

    First, I created a login page, essentially stealing from Tutorial 10 as you will see in the video.
    login.php
       1    <?php
       2    include(“connect.php”);
       3    $errortxt = ;
       4    if(isset($_REQUEST['sub'])){
       5        $username = trim($_REQUEST['username']);
       6        $password = trim($_REQUEST['password']);
       7        //make it safe to see if in the table
       8        $username = mysql_real_escape_string($username);
       9        $password = sha1($password);
      10        //time to query
      11        $sql = “SELECT * FROM `users` WHERE username = ’$username‘ AND password = ’$password‘”;
      12        $result = mysql_query($sql);
      13        $exists = false;
      14        $userid = -1;
      15        while($row = mysql_fetch_array($result)){
      16            $exists = true;
      17            $userid = (int)$row[0];
      18            break;//get out of the while loop
      19        }
      20        if($exists){
      21            $_SESSION['isin'] = 1;
      22            $_SESSION['userid'] = $userid;
      23            header(‘location: blog.php’);
      24        }else{
      25            $errortxt .= “You got something wrong, try again<br />\n”;
      26        }
      27    }
      28    
      29    
      30    
      31    echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
      32    ?>
      33    <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Strict//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
      34    <html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”>
      35    <head>
      36        <title>Blog title</title>
      37        <link href=“main.css” type=“text/css” media=“screen” rel=“stylesheet” />
      38    </head>
      39    <body>
      40        <div class=“maincontent”>
      41            <div class=“top”>
      42                Blog title</div>
      43            <div class=“login”>
      44                <form action=“?” method=“post”>
      45            Username <input type=“text” name=“username” /><br />
      46            Password <input type=“password” name=“password” /> <br />
      47            <input type=“submit” value=“Log in” />
      48            <input type=“hidden” name=“sub” value=“1″ />    
      49        </form>
      50                <div class=“error”><?php
      51                echo $errortxt;
      52                ?></div>
      53            </div>
      54        </div>
      55    </body>
      56    </html>

    Then, I figured, I needed a way to log out. This part is the easiest, as it only includes starting the session essentially, then killing it, and redirecting to the front page.
    logout.php
       1    <?php
       2    include(‘connect.php’);
       3    session_destroy();
       4    header(‘location: blog.php’);
       5    ?>

    So now what?
    Well, now we need to take our login page and our logout page and connect them to the nucleus of our blog, blog.php

    You will notice the added content in lines 53-67 in the following
    blog.php
       1    <?php
       2    include(“connect.php”);
       3    
       4    echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;
       5    ?>
       6    <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Strict//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
       7    <html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”>
       8    <head>
       9        <title>Blog title</title>
      10        <link href=“main.css” type=“text/css” media=“screen” rel=“stylesheet” />
      11    </head>
      12    <body>
      13        <div class=“maincontent”>
      14            <div class=“top”>
      15                Blog title</div>
      16            <div class=“posts”>
      17                <?php
      18                $sql = “SELECT posts.ID as `ID`,
      19    users.postname as `postname`,
      20    posts.title as `title`,
      21    posts.content as `content`,
      22    posts.date as `date` 
      23    
      24    FROM posts 
      25    
      26    INNER JOIN users 
      27    
      28    ON users.ID = posts.username
      29    
      30    ORDER BY date DESC”;
      31                $result = mysql_query($sql);
      32                while($row=mysql_fetch_array($result)){
      33                ?>
      34                <div class=“post”>
      35                    <span class=“author”><?php
      36                    echo $row['postname'];
      37                    ?></span> Wrote 
      38                    <a class=“title” href=“post.php?post=<?php
      39                    echo htmlentities($row['ID']);
      40                    echo ‘”>’;
      41                    echo htmlentities($row['title']);
      42                    ?></a> at <?php
      43                    echo date(‘l jS \of F Y h:i:s A’,(int)$row['date']);
      44                    ?>
      45                    <div class=“content”><?php
      46                    echo htmlentities($row['content']);
      47                    ?></div>
      48                </div>
      49                <?php
      50                }
      51                ?>
      52            </div><!– end of posts section –>
      53            <div class=“controls”>
      54                <?php
      55                if((int)$_SESSION['isin']){
      56                ?>
      57                Hello, You may
      58                <ul>
      59                    <li><a>Post</a></li>
      60                    <li><a href=“logout.php”>Logout</a></li>
      61                </ul> 
      62                <?php
      63                }else{
      64                    echo ‘You may log in through <a href=”login.php”>this</a>.’;
      65                }
      66                ?>
      67            </div>
      68        </div>
      69    </body>
      70    </html>

    In concept, I detected whether logged in or not. If logged in, I would provide appropriate links, if not, I would provide the link to the login page.

    The next tutorial will be going over the creation of a post with a What You See Is What You Get editor.

    You may download the sources bundled in this Zip Archive for your own use.

    Posted by Kloplop321 @ 10:17 pm

    Tags: , , , , , ,

One Response

WP_Orange_Techno

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.