I’ve decided that instead of making a tutorial that I'm going to show you some cool scripts that will make your website more dynamic. I am assuming you know at this point how to use scripts even if you don’t necessarily know how to make them. I’m also assuming you have a phpbb forum and you know how to set it up.
Forum Login Script
You may have noticed that DQ has one of these on pages that aren’t part of the forum, the script basically checks to see if the user is registered and if their session is still active, if so it’ll display either some text or an image or whatever you want, if not it’ll display the login form necessary.
Here’s the script
PHPBB Forum News posts displayed on the front pageCode:<?php define('IN_PHPBB', true); $phpbb_root_path = './forum/'; $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); $user->session_begin(); $auth->acl($user->data); $user->setup(); $username = $user->data['username']; $userid = $user->data['user_id']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Are You Logged In?</title> </head> <body> <? if($user->data['is_registered']){ echo "Hello ".$username.", whatsup?"; } else { echo 'Login Below: <form method="POST" action="forum/ucp.php?mode=login"> <p>Username: <input type="text" name="username" size="40"></p> <p>Password: <input type="password" name="password" size="40"></p> <p>Remember Me?: <input type="checkbox" name="autologin"></p> <p><input type="submit" value="Submit" name="login"></p> </form>'; } ?> </body> </html>
This bugged me for a while until I ran across a script that displayed recent forum titles and with a little tweaking I modified it to display recent threads from one particular forum section. (like a news section)
What this particular script does is display the thread title, who made it and when, the actual post and the number of comments….
Save this as latest.php or something like that and call it on what ever page you want like so...Code:<table width="500" border="0" align="left" cellpadding="0" cellspacing="0" class="style3"> <tr> <td width="600" align="left" valign="top"><?php // How Many Topics you want to display? $topicnumber = 5; // Change this to your phpBB path $urlPath = "/forum"; // Database Configuration (Where your phpBB config.php file is located) include 'forum/config.php'; $table_topics = $table_prefix. "topics"; $table_forums = $table_prefix. "forums"; $table_posts = $table_prefix. "posts"; $table_users = $table_prefix. "users"; $table_post_text = $table_prefix. "message"; $link = mysql_connect("$dbhost", "$dbuser", "$dbpasswd") or die("Could not connect"); mysql_select_db("$dbname") or die("Could not select database"); $query = "SELECT t.topic_id, t.topic_title, t.topic_last_post_id, t.forum_id, p.post_id, p.poster_id, p.post_time, u.user_id, u.username, u.user_avatar, p.post_text, t.topic_replies FROM $table_topics t, $table_forums f, $table_posts p, $table_users u WHERE t.topic_id = p.topic_id AND f.forum_id = t.forum_id AND t.forum_id = 5 AND //this part is the forum id and will vary between forums t.topic_status <> 2 AND p.post_id = t.topic_first_post_id AND p.poster_id = u.user_id ORDER BY p.post_id DESC LIMIT $topicnumber"; $result = mysql_query($query) or die("Query failed"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<h2>" . $row["topic_title"] . "</h2> by: <a href=\"$urlPath/memberlist.php?mode=viewprofile&u=$row[user_id]\" TARGET=\"_self\">" . $row["username"] . "<a/><br />" . " on: " . date('F j, Y, g:i a', $row["post_time"]) . "</a><br />" . $row["post_text"] . "<br /><a href=\"$urlPath/viewtopic.php?f=5&t=$row[topic_id]\" TARGET=\"_self\">Comments " . $row["topic_replies"] . "</a>"; } mysql_free_result($result); mysql_close($link); ?> </td> </tr> </table>
I regret to say that this code does not take care of BB code parsing so if you put aCode:<? require('latest.php'); ?>face or bold some text it won’t come out correctly, I’m trying to get it working but I’m sure you can live with this for now.
Mod Rewrites using htaccess
Htaccess is a great tool in web development that you may not have heard of before, it basically controls how the browser and pages work on your server.
There are a ton of things you can do with it but here I’m just going to show you how to rewrite page urls.
I’m sure you’ve come to the point where you have “/about.html” or “/apage.php” and wanted to remove the url extensions but you were probably stumped on how to deal with them.
With htaccess you can turn /about.html into /about or /apage.php into /apage. Open a new text document and save it as .htaccess, then you can start editing it.
Here is the code for the url extensions…
What this does is removes the .html or .php or .swf to any file that has those extensions and you can add even more file types if you need to.Code:RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.swf -f RewriteRule ^(.*)$ $1.swf
You can also redirect pages automatically if some one clicks a link to some where else.
To redirect a page add this code…
Comment scriptCode:Redirect 301 /memberlist.php?mode=viewprofile&u=53 http://www.mywebsite/apage.php
Every one uses them so why shouldn’t you?
I’ve looked all over for great, free scripts and came across a nice one but it didn’t do quite what I wanted. Since I was trying to make my site more dynamic I didn’t want the user to have to put in their credentials every time, so I modified it to only show the comment box if the user was logged in, detect the users name and fill it in automatically, and link the user to their phpbb profile.
Before you can do it though you’ll have to do some MySQL work which shouldn’t be too difficult if you have already successfully installed a phpbb forum.
Make a new Database called comments and add the following sql
Now that you’ve got the database set up it’s time to use the scripts…Code:CREATE TABLE `comments` ( `commentid` int(11) NOT NULL auto_increment, `tutorialid` int(11) NOT NULL default '0', `name` text NOT NULL, `url` text NOT NULL, `comment` text NOT NULL, `email` text NOT NULL, `date` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`commentid`), KEY `tutorialid` (`tutorialid`) )
This is the actual comment script
Name this comments.php or whatever you want, also this comment script will parse bbcode since it’s being directly added to the page not being called from another.Code:<?php $db_hostname = "localhost"; $db_username = "username"; //edit value $db_pass = "password"; //edit value $db_name = "comments"; $dbh = mysql_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ($db_name); ?><?php define('IN_PHPBB', true); $phpbb_root_path = './forum/'; //edit value $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); $user->session_begin(); $auth->acl($user->data); $user->setup(); $username = $user->data['username']; $userid = $user->data['user_id']; $useravatar = $user->data['user_avatar']; $useravatartype = $user->data['user_avatar_type']; $useravatarwidth = $user->data['user_avatar_width']; $useravatarheight = $user->data['user_avatar_height']; ?> <style type="text/css"> <!-- body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; } .bold { font-weight: bold; } .italics { font-style: italic; } .underline { text-decoration: underline; } .strikethrough { text-decoration: line-through; } .overline { text-decoration: overline; } .sized { text-size: } .quotecodeheader { font-family: Verdana, arial, helvetica, sans-serif; font-size: 12px; font-weight: bold; } .codebody { background-color: #FFFFFF; font-family: Courier new, courier, mono; font-size: 12px; color: #006600; border: 1px solid #BFBFBF; } .quotebody { background-color: #FFFFFF; font-family: Courier new, courier, mono; font-size: 12px; color: #660002; border: 1px solid #BFBFBF; } .listbullet { list-style-type: disc; list-style-position: inside; } .listdecimal { list-style-type: decimal; list-style-position: inside; } .listlowerroman { list-style-type: lower-roman; list-style-position: inside; } .listupperroman { list-style-type: upper-roman; list-style-position: inside; } .listloweralpha { list-style-type: lower-alpha; list-style-position: inside; } .listupperalpha { list-style-type: upper-alpha; list-style-position: inside; } --> </style> <?php function BBCode($Text) { $Text = str_replace("<", "<", $Text); $Text = str_replace(">", ">", $Text); $URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'"; $MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@"; if (substr($Text,0, 7) == "http://"){ $Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>", $Text); $Text = nl2br($Text); } else { $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="javascript:go(\'$1\',\'new\')">$1</a>', $Text); $Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", '<a href="javascript:go(\'$1\',\'new\')">$2</a>', $Text); $Text = nl2br($Text); } $Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $Text); $Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", '<a href="mailto:$1">$2</a>', $Text); $Text = preg_replace("(\[b\](.+?)\[\/b])is",'<span class="bold">$1</span>',$Text); $Text = preg_replace("(\[i\](.+?)\[\/i\])is",'<span class="italics">$1</span>',$Text); $Text = preg_replace("(\[u\](.+?)\[\/u\])is",'<span class="underline">$1</span>',$Text); $Text = preg_replace("(\[s\](.+?)\[\/s\])is",'<span class="strikethrough">$1</span>',$Text); $Text = preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text); $Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text); $Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1px\">$2</span>",$Text); $Text = preg_replace("/\[list\](.+?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text); $Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text); $Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", '<ul class="listlowerroman">$1</ul>' ,$Text); $Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text); $Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text); $Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text); $Text = str_replace("[*]", "<li>", $Text); $Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text); $CodeLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td class="quotecodeheader"> Code:</td> </tr> <tr> <td class="codebody">$1</td> </tr> </table>'; $Text = preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout", $Text); $QuoteLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td class="quotecodeheader"> Quote:</td> </tr> <tr> <td class="quotebody">$1</td> </tr> </table>'; $Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout", $Text); $Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="$1">', $Text); $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" height="$2" width="$1">', $Text); return $Text; } function formatDate($val) { list($date, $time) = explode(" ", $val); list($year, $month, $day) = explode("-", $date); list($hour, $minute, $second) = explode (":", $time); return date("l, m.j.y", mktime($hour, $minute, $second, $month, $day, $year)); } function getComments($tutid){ echo " <style> /*COMMENTS *------------------------------------*/ .postedby { padding: 0 0 0 18px; background: url(images/abullet.gif) no-repeat 0 4px; } h3.formtitle { margin : 0px 0px 0px 0px; border-bottom: 1px dotted #ccc; padding-bottom: 8px; } .commentbody { border-top: 1px dotted #ccc; } /*gray box*/ .submitcomment, #submitcomment, #currentcomments, #rating, .textad { background-color: #F5F5F5; border: 1px dotted #ccc; padding: 5px; padding: 5px; margin: 20px 0px 0px 0px; } /*FORMS *------------------------------------*/ .form { font-size: 70%; background-color: #FAFAFA; border: solid 1px #C6C6C6; padding: 2px; } .formtext { background-color: #FAFAFA; border: solid 1px #C6C6C6; padding: 2px; border-bottom: 1px dotted #ccc } .form:hover, .formtext:hover { background: white; } .form:focus, .formtext:focus { background: white; border: solid 1px #000000; } .submit { background-color: #D3D3D3; border: solid 1px #C6C6C6; border-right: solid 1px #9A9A9A; border-bottom: solid 1px #9A9A9A; } .submit:hover, .submit:focus { background: #EDEDED; } </style> "; $commentquery = mysql_query("SELECT * FROM comments WHERE tutorialid='$tutid' ORDER BY date") or die(mysql_error()); $commentNum = mysql_num_rows($commentquery); echo "<div id=\"currentcomments\" class=\"submitcomment\"><h3 class=\"formtitle\">Current Comments</h3>\n"; echo $commentNum . " comments so far (<a href=\"#post\">post your own</a>)\n"; while($commentrow = mysql_fetch_row($commentquery)){ $commentbb = BBCode($commentrow[4]); $commentDate = formatDate($commentrow[6]); echo "<div class=\"commentbody\" id=\"$commentrow[0]\">\n <p>$commentbb</p>\n <p class=\"postedby\">Posted by "; if($commentrow[3]){ echo "<a href=\"$commentrow[3]\">$commentrow[2]</a> "; } else { echo "$commentrow[2] "; } echo "on $commentDate </p>\n \n</div>"; } echo "</div>"; } function submitComments($tutid2,$tuturl){ ?> <script language="javascript"> function form_Validator(form) { if (form.message.value == "") { alert("Please enter your message."); form.message.focus(); return (false); } return (true); } //--> </script> <?php echo " <a name=\"post\"> <div id=\"submitcomment\" class=\"submitcomment\"> <form name=\"submitcomment\" method=\"post\" action=\"submitcomment.php\" onSubmit=\" return form_Validator(this)\"> <table width=\"100%\"> <tr> <th colspan=\"2\"><h3 class=\"formtitle\">Leave your comment:</h3></th> </tr> <tr valign=\"top\"> <th scope=\"row\"><p class=\"req\">Comments:</p><br /></th> <td><textarea class=\"formtext\" tabindex=\"4\" id=\"message\" name=\"message\" rows=\"10\" cols=\"50\"></textarea></td> </tr> <tr> <td> </td> <td><input type=\"submit\" name=\"post\" class=\"submit\" value=\"Submit Comment\" /><br /> </td> </tr> </table> <input type=\"hidden\" name=\"tuturl\" value=\"$tuturl\" /> <input type=\"hidden\" name=\"tutid2\" value=\"$tutid2\" /> </form> </div> "; } ?>
Now for the script that will handle the message submitted
Name it submitcomment.phpCode:<?php define('IN_PHPBB', true); $phpbb_root_path = './forum/'; //edit value $phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); $user->session_begin(); $auth->acl($user->data); $user->setup(); $username = $user->data['username']; $userid = $user->data['user_id']; ?> <?php $db_hostname = "localhost"; $db_username = "username"; //edit value $db_pass = "password"; //edit value $db_name = "comments"; $dbh = mysql_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ($db_name); $name = $username; $url = '/forum/memberlist.php?mode=viewprofile&u='.$userid.''; $email = $_POST["email"]; $message = $_POST["message"]; $av = $useravatar; $sendcomment = mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now()"); if($sendcomment){ echo "<h1>Submission Successful</h1>"; echo "Your comment has been submitted. You will now be redirected back to the last page you visited. Thanks!"; $ref = $_SERVER['HTTP_REFERER']; echo "<meta http-equiv='refresh' content='2;URL=$ref'>"; } else { echo "There was an error with the submission. "; } ?>
What this does is it validates the message and replaces the blank username area with your forum username and links your name to your forum profile. It also will redirect you back to the previous page.
Now this is all fine and dandy but how do you add it to a page?
Well simply do the following…
I guess that’s all for right now, remember there’s a lot more things you can do to your website and hopefully this article got you started.Code:<html> <head> <title>Test Page</title> </head> <body> <h1>Test page</h1> <p>Here is the content...</p> <? require(comments.php'); getComments("1"); // if you edit this value it will add a blank comments page, so if you use 1 for this page you must use 2 for the next and so on. ?> <? if($user->data['is_registered']){ submitComments("1","$PHP_SELF"); // also edit the number here } else { echo "You must be logged in to leave a comment.";} ?> </body> </html>
Links to scripts I used/edited
http://www.zimmertech.com/tutorials/...t-tutorial.php
http://zachkrasner.com/stp-article:219
http://softinquiry.com/latest-posts-...nd-phpbb2.html
For more reading check out these sites
http://www.php.net
http://www.mysql.com/


face or bold some text it won’t come out correctly, I’m trying to get it working but I’m sure you can live with this for now.
Reply With Quote