Tuesday, May 12, 2009

Hosting various websites on one server with PHP

If you want to host various websites that have their own domain name on your own server then this is how you do that with PHP.

Suppose you have two domainnames blog.serveftp.com and wiki.serveftp.com. You want your server to redirect incoming calls to the proper website that you host yourself. You have set up 2 domains called: www.my_blog.com and www.my_wiki.com. On your server you have set up 2 directories in the root that contain your blog software and your wiki software.

Create an index.php file in your root that goes as follows

<?php
if ( getenv("HTTP_Host") == "www.my_blog.com" ) {
header("Location: http://your_ip/wordpress");
} elseif (getenv("HTTP_Host") == "www.my_wiki.com") {
header("Location: http://your_ip/wiki");
}
?>

depending on how you set up your blog and wiki software, you may need to include a startup file to your redirect address.

This is also possible if your connection with the internet is a dynamic IP number:

In that case take out a free account on http://www.no-ip.com e.g. http://my_name.serveblog.com and install their agent.

 

Your redirects in the above code then become http://my_name.serveblog.com/wordpress and http://my_name.serveblog.com/wiki

Alternating colors in MySQL tabel with PHP

This code changes the color of alternating rows of a database output. There is a difference in results if used with Firefox as opposed to IE. In the former, no white grid will be visible while in the latter, a white grid will be visible as shown below

 

<?php
$dbhost = 'localhost'; // choose as needed 
$dbuser = 'xxx';  // choose as needed 
$dbpass = 'yyyy';  // choose as needed 
$dbname = 'DVD';  // choose as needed 
$tablename = 'DVD';  //choose as needed

//Connect to server and select database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("No connection");
//mysql_connect($dbhost, $dbuser, $dbpass) or die("No Connection");
mysql_select_db($dbname);

//Do the SQL
$query=("SELECT * FROM $tablename  ORDER BY Titel ASC ")  or die (mysql_error());
$result=mysql_query($query);

//variables for the table
$count=1;

// for greyish table
$akleur1="#CCCCCC";
$akleur2="#C0C0C0";

// uncomment and use for colorfull table 
// $akleur1="#C6FF00";
// $akleur2="#FFC600";

$kleur=$akleur1;

//Table heading and loop
echo"<table border='0'><tbody>";
echo"<tr><td><b>Nr</b></td><td><b>Titel</b></td><td><b>Acteur</b></td><td><b>Jaar</b></td><td><b>Summary</b></td><td>IMDB</td></tr>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

    echo "<tr bgcolor=$kleur><td>$count</td><td>{$row['Titel']}</td>" .
         "<td><em>{$row['Acteur']}</em></td>" .
         "<td>{$row['Jaar']}</td>" .
         "<td>{$row['Summary']}</td>" .
         "<td><a href='{$row['IMDB']}'>imdb</a><td></tr>";

$count=$count+1;

if($kleur==$akleur1){
$kleur=$akleur2;
}
else {
$kleur=$akleur1;
}
}
echo "</tbody></table>";

// Close database
echo mysql_error();
mysql_close($conn);
?>