Archive for the 'Apache' Category

Hosting your own web site - WRITE YOUR OWN BLOG ENGINE if($mycomments) { echo

Tuesday, January 15th, 2008

WRITE YOUR OWN BLOG ENGINE if($mycomments) { echo “

“; do { $comment_id = $mycomments[”comment_id”]; $name = $mycomments[”name”]; $website = $mycomments[”website”]; $comment = format($mycomments[”comment”]); if ($website != “”) { echo “
$name wrote:

\n”; } else { echo “

$name wrote:

\n”; } echo “

$comment

\n”; } while ($mycomments = mysql_fetch_array($result3)); echo “

“; } else { echo “

There are no comments yet.

“; } ?>

This code should be quite familiar to you by now. First, it checks that some comments have been found in the database and then uses a do-while loop to display them on the screen, adding a link to the commenter s name if one was supplied. Notice also that I am using the format function to format the comment. Figure 7-17 shows what your post page should like after a comment has been added. Figure 7-17. Post page with a comment added 317

BLOG DESIGN (Best web hosting site) SOLUTIONS The subject, body, and headers

Monday, January 14th, 2008

BLOG DESIGN SOLUTIONS The subject, body, and headers are constructed using the following code: $emailsubject = “Comment added to: “.$posttitle; $emailbody = “Comment on ‘”.$posttitle.”‘”.”\r\n” .”http://www.your-domain-name.com/post.php?post_id=”.$post_id .”#c”.$comment_id.”\r\n\r\n” .$comment.”\r\n\r\n” .$name.” (”.$website.”)\r\n\r\n”; $emailbody = stripslashes($emailbody); $emailheader = “From: “.$name.” <".$email.">\r\n”.”Reply-To: “.$email; @mail(”you@your-domain-name.com”, $emailsubject, $emailbody, $emailheader); Remember to replace www.your-domain-name.com with your website domain, and you@your-domain-name.com with your e-mail address. Note also the @ in front of the mail function. This prevents any errors being written to the screen if for some reason the mail function fails. And the final line of the comment insert script is as follows: header(”Location: post.php?post_id=$post_id&message=$message”); The header function creates an HTTP header, which is essentially an instruction to the web server. In this case, the instruction is to go to the post page with a success message appended to the query string. This step prevents readers from submitting their comment twice if they click reload in their browser. So now readers can add comments to your posts, and you are notified each time that happens. The final step is to show those comments in your blog. Let s look at the script that will pull the comments from the database. Add this script to the end of PHP code at the top of post.php: if ($myposts) { $sql = “SELECT comment_id, name, website, comment FROM comments . WHERE post_id = $post_id”; $result3 = mysql_query($sql); $mycomments = mysql_fetch_array($result3); } If the post exists, the comments for this post are pulled from the database. Next you can display them on the page by inserting this code into the posts div, just after the closing ?> delimiter in your post loop:

Comments

Cedant web hosting - WRITE YOUR OWN BLOG ENGINE // direct to

Monday, January 14th, 2008

WRITE YOUR OWN BLOG ENGINE // direct to post page to eliminate repeat posts header(”Location: post.php?post_id=$post_id&message=$message”); } } This code inserts a new comment into the database and e-mails you when a new comment is added. Stepping through the code: if (isset($_POST[”postcomment”]) != “”) { If the Post comment button has been pressed, the comment can be added to the database. $posttitle = addslashes(trim(strip_tags($_POST[”posttitle”]))); $name = addslashes(trim(strip_tags($_POST[”name”]))); $email = addslashes(trim(strip_tags($_POST[”email”]))); $website = addslashes(trim(strip_tags($_POST[”website”]))); $comment = addslashes(trim(strip_tags($_POST[”comment”]))); The post title (sent as a hidden field), commenter s name, e-mail address, website, and comment are all gathered from the query string and are tidied up by nesting three functions. Firstly strip_tags eliminates any HTML and PHP from the comment input, then trim removes white space (such as spaces and line breaks) from either end of the input, and finally addslashes ensures the text is formatted suitably for database entry. $sql = “INSERT INTO comments (post_id,name,email,website,comment) VALUES (’$post_id’, ‘$name’, ‘$email’, ‘$website’, ‘$comment’)”; $result2 = mysql_query($sql); if (!$result2) { $message = “Failed to insert comment.”; } else { $message = “Comment added.”; The database insert is performed. Note that the post_id is inserted with the comment so in future the blogging engine will know which comments go with which posts. $comment_id = mysql_insert_id(); As when inserting a new post, the comment_id for a new comment is automatically generated by MySQL. The latest id can be retrieved using the mysql_insert_id function. Now comes part of the script which e-mails yourself the comment so you know as soon as someone comments on your blog. PHP uses the mail function to send e-mail. An e-mail is made up of 4 four parts: the to address, the subject, the body, and the headers (which contain additional information such as the sender s details and cc d addresses). The mail function works like this: mail(to address, subject, body, headers) 315

BLOG DESIGN (Web server application) SOLUTIONS } ?> Name: Email: Website:

Sunday, January 13th, 2008

BLOG DESIGN SOLUTIONS } ?>

Name:

Email:

Website:

Comment:

Now add the following script to the end of the PHP code (just before the closing ?> delimiter) at the top of your document: // If comment has been submitted and post exists then add comment to database if (isset($_POST[”postcomment”]) != “”) { $posttitle = addslashes(trim(strip_tags($_POST[”posttitle”]))); $name = addslashes(trim(strip_tags($_POST[”name”]))); $email = addslashes(trim(strip_tags($_POST[”email”]))); $website = addslashes(trim(strip_tags($_POST[”website”]))); $comment = addslashes(trim(strip_tags($_POST[”comment”]))); $sql = “INSERT INTO comments (post_id,name,email,website,comment) VALUES (’$post_id’, ‘$name’, ‘$email’, ‘$website’, ‘$comment’)”; $result2 = mysql_query($sql); if (!$result2) { $message = “Failed to insert comment.”; } else { $message = “Comment added.”; $comment_id = mysql_insert_id(); // Send yourself an email when a comment is successfully added $emailsubject = “Comment added to: “.$posttitle; $emailbody = “Comment on ‘”.$posttitle.”‘”.”\r\n” .”http://www.your-domain-name.com/post.php?post_id=”.$post_id . .”#c”.$comment_id.”\r\n\r\n” .$comment.”\r\n\r\n” .$name.” (”.$website.”)\r\n\r\n”; $emailbody = stripslashes($emailbody); $emailheader = “From: “.$name.” <".$email.">\r\n”.”Reply-To: ” . .$email; mail(”you@your-domain-name.com”, $emailsubject, $emailbody, . $emailheader);

WRITE YOUR OWN BLOG ENGINE Figure 7-15. Creating

Saturday, January 12th, 2008

WRITE YOUR OWN BLOG ENGINE Figure 7-15. Creating a new table On the next screen, input the table definition for each field, as shown in Figure 7-16. Note that for the comment_id field you should also select auto_increment in the Extra column and click the primary column. This procedure ensures that each new comment automatically gets its own unique id number. 7 Figure 7-16. Creating the comments table in phpMyAdmin Click Save and the table should be created you will see comments added to the left column. Now that you have a database table in which to store comments, you can add a form to collect comments and a script to insert to those comments into the database. Add this form to the sidebar in post.php, just after the searchform include:

” method=”post”>

“.$_POST[”message”].”

“; 313

Make my own web site - BLOG DESIGN SOLUTIONS As with the edit post

Saturday, January 12th, 2008

BLOG DESIGN SOLUTIONS As with the edit post page in your CMS, the blog post page is called with a post_id in the query string so it knows which blog post to display. The blog homepage (index.php) you just created links to the post page like this: post.php?post_id=n where n is the post_id of a post. Clicking a link on your homepage should take you to a blog post that looks something like Figure 7-14 (if you haven t already, make sure you have added some blog posts using your CMS). Figure 7-14. The post page for your first blog post At this point, you could have a little play by adding a few new posts through your CMS and see how they appear on the homepage and on their individual post pages. Adding comments One of the great attractions of blogging is allowing your readers to comment on what you have written. I ll now show you how to add commenting to your blog engine. Each comment will be stored in the database, so first you need to create a table in which to store the comments. Open up PHPMyAdmin in your browser. Select the database blog in the left side and then click the Structure tab. In the Create new table on database blog area, type comments into the Name input box and 7 into the Number of fields input box, as shown in Figure 7-15, then click Go.

WRITE YOUR OWN BLOG ENGINE (Web design) Samuel’s Blog @import

Friday, January 11th, 2008

WRITE YOUR OWN BLOG ENGINE

$title

\n”; echo “

Posted on $dateattime

\n”; echo “

\n $post \n

“; } while ($myposts = mysql_fetch_array($result)); } else { echo “

There is no post matching a post_id of $post_id.

“; } ?>

311

BLOG DESIGN SOLUTIONS if($myposts) { echo “\n”; do (Sex offenders web site)

Thursday, January 10th, 2008

BLOG DESIGN SOLUTIONS if($myposts) { echo “

    \n”; do { $post_id = $myposts[”post_id”]; $title = $myposts[”title”]; echo “
  • . $title
  • \n”; } while ($myposts = mysql_fetch_array($result)); echo “

“; } ?>

Creating a post page In a blog, every post should have its own page. To enable this you will combine code from the blog homepage (index.php) and the CMS edit post page (addpost.php). Save this code as post.php in the root of your blog directory:

Posted in Apache | No Comments »

Michigan web site - WRITE YOUR OWN BLOG ENGINE Samuel’s Blog @import

Wednesday, January 9th, 2008

WRITE YOUR OWN BLOG ENGINE

. $title

\n”; echo “

Posted on $dateattime

\n”; echo “

$post

“; } while ($myposts = mysql_fetch_array($result)); } else { echo “

I haven’t posted to my blog yet.

“; } ?>

BLOG DESIGN SOLUTIONS Headers, footers, and other reusable (Web hosting contract)

Wednesday, January 9th, 2008

BLOG DESIGN SOLUTIONS Headers, footers, and other reusable elements In the previous section, you created an include file called functions.php that can be reused in any page of your blog. It is useful to put all repeated parts of your code into includes so they can be easily updated if site-wide changes are required further down the line. The header, footer, and search form used in the index.php page will be reused throughout the site, so create the following include files. First, save this header code as header.php:

And replace this code in index.php with . Now do the same with the footer by creating footer.php from this code:

And create searchform.php from this code:

View the Archive

After you replace the footer and search code in your index.php file, you should end up with this complete source listing: