Quick and Easy PHP Based Web Portal

Note: Before you read this article you may want to read my HOWTO on installing Apache, PHP, Mysql, and ModGZIP unless you already have a webhost that uses Apache with PHP and MySQL. You will also need some basic HTML code experience (to change my code from barebones crap, to a nice looking site), some basic PHP coding experience (to modify my code, and add more features, only if you want to though), and of course, a webserver (preferably apache) that uses PHP and MySQL.
And, if your using PHP version 4.2.1 you must turn register_globals on in /etc/php.ini or /usr/local/lib/php.ini or some of the scripts wont work. This was disabled by the php development team by default.

I am going to give you the basic code needed to start a basic PHP based Web Portal, you can leave it if you like it, or modify it to how you want it. There will be 7 small php scripts that you will be able to include anywhere on your site. The 7 scripts will be (read, add, and search news, read, add, and search links, an email form)

I am going to start by giving you all the code (PHP and HTML) and all the commands to create the databases you need. Everything you need to type in, will be in Small Red Italics.

You will now be able to post news, and allow other users to post news on your website. I will start by showing you how to create the database.
Open up mysql, and create a new database titled ‘portal’ by typing:

create database portal;

Now open up the database so you can insert a table into it, the table will be called ‘news’ for simplicity. You will open up the database by typing:

use portal;

Now you will create the database by typing the following line:

create table news(s_head text, s_body longtext, username text, email text, date datetime, id int(4) DEFAULT ‘0’ NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), UNIQUE id (id));

Congradulations, you have created the first table in your database. Now to create the rest. Please type in the following lines:

create table links(link_title text, link_url text, link_desc text, id int(4) DEFAULT ‘0’ NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), UNIQUE id (id));

There your done creating the database. Close mysql.

Now for the code for each of the programs.

News:

Open up a new text file and call it readnews.php, then insert the following code into it:

<?

$dbhost = “localhost”;
$dbuser = “your_username”;
$dbpass = “your_password”;
$dbname = “portal”;
$table = “news”;

mysql_connect($dbhost,$dbuser,$dbpass) or die (“Unable to connect to database”);
@mysql_select_db(“$dbname”) or die (“Unable to retrieve news from database”);
$sqlquery = “SELECT * FROM $table ORDER by date DESC limit 10”;
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);

$i = 0;

if ($number <1)
{
echo “<center><p>There is currently no news in the database</p></center>”;
}

else
{
while ($number > $i)
{
$s_head = mysql_result($result, $i, “s_head”);
$s_body = mysql_result($result, $i, “s_body”);
$username = mysql_result($result, $i, “username”);
$email = mysql_result($result, $i, “email”);
$date = mysql_result($result, $i, “date”);
$id = mysql_result($result, $i, “id”);
echo “$s_head\n<br>\n<font size=\”-1\”>\nPosted by: <a href=\”mailto:$email\”>$username</a> on $date</font>\n<font size=\”-2\”>NewsID: $id</font>\n<br>\n$s_body\n<br>”;

$i++;
}
}

?>


Now save the file as readnews.php and open up a new file.

postnews.php

<?

$dbhost = “localhost”;
$dbuser = “your_username”;
$dbpass = “your_password”;
$dbname = “portal”;
$table = “news”;

mysql_connect($dbhost,$dbuser,$dbpass) or die (“Unable to access the database”);
@mysql_select_db(“$dbname”) or die (“Unable to find the table $table”);
$sqlquery = “INSERT INTO $table VALUES (‘$s_head’,’$s_body’,’$username’,’$email’,now(),0)”;
$results = mysql_query($sqlquery);
echo “<html>Thankyou, your news has been submitted.</html>”;
mysql_close();

?>


Now save the file as postnews.php and open up a new file.

postnews.html

<HTML>
<head>
<title>Post your own news</title>
</head>
<body>
<table width=”99%”>
<tr>
<td align=”left”>
News Headline:
</td>
<td align=”left”>
<FORM NAME=”s_head” ACTION=”postnews.php”>
<INPUT TYPE=”text” NAME=”s_head” SIZE=”50″>
</td>
</tr>
<tr>
<td align=”left”>
News Body:
</td>
<td align=”left”>
<FORM NAME=”s_body” ACTION=”postnews.php”>
<TEXTAREA cols=”45″ rows=”5″ name=”s_body”></textarea>
</td>
</tr>
<tr>
<td align=”left”>
Your Name:
</td>
<td align=”left”>
<FORM NAME=”username” ACTION=”postnews.php”>
<INPUT TYPE=”text” NAME=”username” SIZE=”50″>
</td>
</tr>
<tr>
<td align=”left”>
Email Address:
</td>
<td algin=”left”>
<FORM NAME=”email” ACTION=”postnews.php”>
<INPUT TYPE=”text” NAME=”email” SIZE=”50″>
</td>
</tr>
</table>
<center>
<INPUT TYPE=”submit” VALUE=”Submit News.”>
</center>
</FORM>
</body>
</html>



Now save the file as postnews.html and open up a new file.

searchnews.php

<?

$dbhost = “localhost”;
$dbuser = “your_username”;
$dbpass = “your_password”;
$dbname = “portal”;
$table = “news”;

mysql_connect($dbhost,$dbuser,$dbpass) or die (“Unable to connect to database”);
@mysql_select_db(“$dbname”) or die (“Unable to retrieve news from database”);
$srch = “%”.$search.”%”;
$sqlquery = “SELECT * FROM $table where s_body like ‘$srch’ || s_head like ‘$srch'”;
$result = mysql_query($sqlquery);

echo “Here are the results for your search: <b><i>$search</i></b><br><br>”;
echo “<center>”;

while ($r = mysql_fetch_array($result)) {
$s_head = $r[“s_head”];
$s_body = $r[“s_body”];
$username = $r[“s_username”];
$email = $r[“email”];
$date = $r[“date”];
$id = $r[“id”];
echo “$s_head\n<br>\n<font size=\”-1\”>\nPosted by: <a href=\”mailto:$email\”>$username</a> on $date</font>\n<font size=\”-2\”>NewsID: $id</font>\n<br>\n$s_body\n<br>”;

}
?>



Now save the file as searchnews.php and open up a new file.

searchnews.html

<form method=”post” action=”searchnews.php”>
Search For:<br>
<input type=”text” name=”search” size=”10″><br><br>
<input type=”submit” value=”Search News”><br>
</form>


Now save the file as searchnews.html and open up a new file.

Links

readlink.php

<?

$dbhost = “localhost”;
$dbuser = “your_username”;
$dbpass = “your_password”;
$dbname = “portal”;
$table = “links”;

mysql_connect($dbhost,$dbuser,$dbpass) or die (“Unable to connect to database”);
@mysql_select_db(“$dbname”) or die (“Unable to retrieve links from database”);
$sqlquery = “SELECT * FROM $table”;
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);

$i = 0;

if ($number <1)
{
echo “<center><p>There are currently no links in the database</p></center>”;
}

else
{
while ($number > $i)
{
$link_title = mysql_result($result, $i, “link_title”);
$link_url = mysql_result($result, $i, “link_url”);
$link_desc = mysql_result($result, $i, “link_desc”);
echo “<center><tr><td width=\”150\”><center><a href=\”http://$link_url\” target=\”_new\”>$link_title</a></center></td><td width=\”5\”><b>-</b></td><td>$link_desc</td></tr></center>\n”;

$i++;
}
}

?>



Now save the file as readlink.php and open up a new file.

postlink.php

<?

$dbhost = “localhost”;
$dbuser = “your_username”;
$dbpass = “your_password”;
$dbname = “portal”;
$table = “links”;

mysql_connect($dbhost,$dbuser,$dbpass) or die (“Unable to access the database”);
@mysql_select_db(“$dbname”) or die (“Unable to find the table $table”);
$sqlquery = “INSERT INTO $table VALUES (‘$link_title’,’$link_url’,’$link_desc’,0)”;
$results = mysql_query($sqlquery);
echo “<html>Thankyou for the link, it has been added to the site.</html>”;
mysql_close();

?>

Now save the file as postlink.php and open up a new file.

postlink.html

<HTML>
<head>
<title>Add-A-Link</title>
</head>
<table width=”99%”>
<tr>
<td align=”left”>
Link Title:
</td>
<td align=”left”>
<FORM NAME=”link_title” ACTION=”postlink.php”>
<INPUT TYPE=”text” NAME=”link_title” SIZE=”50″>
</td>
</tr>
<tr>
<td align=”left”>
URL: <font size=”-1″><i>(Dont add http://)</i></font>
</td>
<td align=”left”>

<FORM NAME=”link_url” ACTION=”postlink.php”>
<INPUT TYPE=”text” NAME=”link_url” SIZE=”50″>
</td>
</tr>
<tr>
<td align=”left”>
Short Description:
</td>
<td align=”left”>
<FORM NAME=”link_desc” ACTION=”postlink.php”>
<textarea cols=40 rows=2 name=link_desc></textarea>
</td>
</tr>
</table>
<center>
<INPUT TYPE=”submit” VALUE=”Submit Link.”>
</center>
</FORM>

Now save the file as postlink.html and open up a new file.

searchlink.php

<?

$dbhost = “localhost”;
$dbuser = “your_username”;
$dbpass = “your_password”;
$dbname = “portal”;
$table = “links”;

mysql_connect($dbhost,$dbuser,$dbpass) or die (“Unable to connect to database”);
@mysql_select_db(“$dbname”) or die (“Unable to retrieve links from database”);
$srch = “%”.$search.”%”;
$sqlquery = “SELECT * FROM $table where link_desc like ‘$srch’ || link_title like ‘$srch'”;
$result = mysql_query($sqlquery);

echo “Here are the results for your search: <b><i>$search</i></b>”;
echo “<center>”;

while ($r = mysql_fetch_array($result)) {
$link_title = $r[“link_title”];
$link_url = $r[“link_url”];
$link_desc = $r[“link_desc”];
echo “<center><tr><td width=\”150\”><center><a href=\”http://$link_url\” target=\”_new\”>$link_title</a></center></td><td width=\”5\”><b>-</b></td><td>$link_desc</td></tr></center>\n”
}

?>

Now save the file as searchlink.php and open up a new file.

searchlink.html

<form method=”post” action=”searchlink.php”>
Search For:<br>
<input type=”text” name=”search” size=”10″><br><br>
<input type=”submit” value=”Search Links”><br>
</form>


Now save the file as searchlink.html and open up a new file.

Email Form:

email.php

<?php

$return = $f_mail;
$subject = “Message from Contact Form”;
if ($f_name <> “” and $f_mail <> “” and $f_message <> “”) {
mail(“$f_email”, “$subject”, “From: $f_name\nMail: $f_mail\nMessage:\n\n$f_message”,”Return-Path:<$return>\nFrom:$return”);
header(“header.txt”);
exit;
}

?>

<body bgcolor=”FFFFFF”>
<table width=100%>
<tr><td align=right>Please select the email address you would like to contact.</td>
<td align=left><form method=”POST”>
<p><select size=”1″ name=”f_email”>
<option selected>your.first@emailaddress.com</option>
<option>your.second@emailaddress.com</option>
</select></p>
</td>
</tr>
<tr><td align=right>Your Name:</td>
<td align=left><input type=text name=f_name size=30></td></tr>
<tr><td align=right>Your E-mail</td>
<td align=left><input type=text name=f_mail size=30></td></tr>
<tr><td align=right>Message</td>
<td align=left><textarea cols=40 rows=5 name=f_message></textarea></td></tr>
</table>
<center><input type=submit value=”Send Mail”></center>
</form>
</body>


Now save the file as email.php.

Great, now that you have inserted all the code and made all the proper files, you can now move on to making your site template. Im going to give you the basic look (just so you can see how to insert the scripts you just made), I hope you dont use my template, but make your own (only because Im just going to throw a couple things together, it will probably look pretty brutal :])

Website Template:

Alright, you have either plugged all the code I just gave you into your own files, or you have downloaded the .tar.gz file I have made, now to put all the code to good use. I will give you a basic looking template, just to show you how to place the code. Its simple, really it is 🙂

index.php

<html>
<head>
<title>Welcome to my portal</title>
</head>
<body>
<table width=”100%”>
<tr>
<td width=”150″>
<a href=”readlink.php”>View Links</a><br>
<a href=”postlink.html”>Add your Link</a><br>
<? include (‘searchlink.html’); ?><br>
<a href=”email.php”>Contact Us</a><br><br>
</td>
<td>
<? include (‘readnews.php’); ?><br>
<center><a href=”postnews.html”>Post your own news.</a><br>
<? include (‘searchnews.html’); ?></center>
</td>
</tr>
</table>
</body>
</html>


Save this file as index.php this file MUST be saved as a .php file, or the includes wont work.

There you have no created a basic barebones portal using php and mysql. when you decide to modify the site the only lines of code you are going to have to worry about moving around and stuff will be:

View Links Script – <a href=”readlink.php”>View Links</a><br>
Post Links Script – <a href=”postlink.html”>Add your Link</a><br>
Search Links Script – <? include (‘searchlink.html’); ?><br>
Email Form – <a href=”email.php”>Contact Us</a><br>
Read News Script – <? include (‘readnews.php’); ?><br>
Post News Script – <a href=”postnews.html”>Post your own news.</a><br>
Search News Script – <? include (‘searchnews.html’); ?>

So now all YOU have to do is plug-in your graphics, and text, and start your site going.