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 xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”
35
36 Blog title
37 href=“main.css” type=“text/css” media=“screen” rel=“stylesheet”
38
39
40 class=“maincontent”
41 class=“top”
42 Blog title
43 class=“login”
44 action=“?” method=“post”
45 Username type=“text” name=“username”
46 Password type=“password” name=“password”
47 type=“submit” value=“Log in”
48 type=“hidden” name=“sub” value=“1″
49
50 class=“error”<?php
51 echo $errortxt;
52 ?>
53
54
55
56
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 xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”
8
9 Blog title
10 href=“main.css” type=“text/css” media=“screen” rel=“stylesheet”
11
12
13 class=“maincontent”
14 class=“top”
15 Blog title
16 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 class=“post”
35 class=“author”<?php
36 echo $row['postname'];
37 ?> Wrote
38 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 class=“content”<?php
46 echo htmlentities($row['content']);
47 ?>
48
49 <?php
50 }
51 ?>
52 <!– end of posts section –>
53 class=“controls”
54 <?php
55 if((int)$_SESSION['isin']){
56 ?>
57 Hello, You may
58
59 Post
60 href=“logout.php”Logout
61
62 <?php
63 }else{
64 echo ‘You may log in through <a href=”login.php”>this</a>.’;
65 }
66 ?>
67
68
69
70
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.
September 1st, 2011 at 9:45 pm
[...] See Full Tutorial [...]