Monday, February 1, 2010

Dynamic website tutorials

This is my first time to create a tutorials in web development and i hope this will help some newbie web developer.

In this article, I used PHP server scripting and mySQL as a data repository. This is only a basic dynamic website. You can edit, add and delete pages.

First, we have to create a database. Since this is only a basic, i will only use one table.

1. Create your database. name it myDB.

2. Create the table. Name it myTABLE and create the fields: id [integer][auto increment], pages [varchar 20], title [varchar 255], body[longtext ], date[varchar 20], and author [varchar 20].

3. Put a dummy data on the fields.
id auto number
pages About
title Company Profile
body This can be a paragraph
date [blank]
author einnox

4. Ok. let’s do the coding. First you have to plan the structure and how you can execute a less work. Using reusable codes is one of them.
Create a header and footer page, and include them in your index page:

Now, create the connection. In this tuturial i will put the connection in a one file called conn.php and save it under include folder.

Folder Structure:
wwwroot:
footer.php
header.php
index.php
/include/conn.php

Now, lets do the connection page. Save it at folder /include

Ohhh that sweet. We have already a connection script where we will user entire the page. We just include the conn.php to the page and call the functions mylogin() and mylogout().

ex:

create a html table here for your header

create a html table here for your footer

We will now create the actual page. Save it as index.php
——————————————————–

<\?php
$myquery = “SELECT pages FROM MyTable order by id asc”;
$myresult = mysql_query($myquery) or die (“Error in query: $myquery. ” . mysql_error());

while($willdisp = mysql_fetch_object($myresult))
{
echo “”;
echo “”;
echo ” $willdisp->pages”;
echo “”;
}
}

?\>

<\?php
$q = “SELECT * FROM MyTable WHERE id = ‘$id’”;
$r = mysql_query($q) or die (“Error in query: $q. ” . mysql_error());

$disp = mysql_fetch_object($r);

if($disp) {
echo “$disp->title ”;
echo “”;

echo “”;
echo nl2br($disp->body);
echo “”;
echo “”;
echo “Last updated:”;
echo formatDate($disp->date);
echo “”;
echo “updated by:”
echo “disp->author”;
echo “”;
}
?\>

———————————————–
dont worry, explanation here

The first part is the connection where we only call the connection script conn.php.Then we open the database using the mylogin(); function.

Then we create a html table where we included the header.php

Then the body. The body consist of a table with two columns. first column hold the navigation script, and the second column holds the contents.

On the navigation column you can find the php scripts where the [index.php?id='$id'] is passing a parameter [$id] to itself. If you have a navigation [Home][Services][products][Contacts] and has the corresponding id’s [1][2][3][4] the script will display the content of navigation when the id parameter will be pass along. Quiet cool, but we have only the index.php which the content are dynamically interchanging everytime the id is changing. This kind of website can hold may navigation link, just add on the database and its automatically display on the index page.

So now? you owe me 5 cent This is calle da spagheti code.

What is Host Header?

I am always asked about this. So i will give some explaination.

Let’s go!!!
HOST HEADER is a term in web servers, thats for sure. . If your are hosting many websites in a single machine or IP, of course you used host header. Host Header is the one who identify or map the website from the domain to the host server. For example: you have a domain NAGLAINGKA.COM registered in onlinenic or other DNS servers. This domain naglaingka.com has a dns pointer like “NS1.serverko.com and ns2.serverko.com”. You can edit the “DNS” by pointing it to your host IP.


Scenario:
1. you have domain naglaingka.com and nalalaingka.com
2. You have a webserver with public ip of 192.168.0.1

Solutions:
1. put the webpages of naglaingka.com in a folder naglaingka on the host computer.
2. put the webpages of nalalaing.com in a folder nalalaingka on the host computer.

3. login to your DNS provider and edit the DNS for nalaingka.com.

host = “blank”
type = A
data = 192.168.0.1

host = www
type = A
data = 192.168.0.1

do this also at nalalainglka.com

Note: if you notice a created 2 pointer, 1 for the blank and 1 for the www. This simply means that if you type naglaingka.com without www, still it will resolve to the IP. So you can access the website with or without the www prefix.

4. Now login to your host computer with 192.168.0.2 Internet Protocol and the ff:

4.1. Go to your Internet Information Services or simply IIS
4.2. Right click and choose New Site and click Next
4.3. Type the Description of the site
4.4. IP and Port Settings

- all unassigned
- port = 80
- hostheader = naglaingka.com
- Path = d:\naglaingka
- read only

Create again a host header for www
a. Go to the properties of the website you created
b. On the website Tab, click the advanced button
c. click Add on the multiple Identities
d. All unassigned, port = 80, hostheader = www.naglaingka.com
e. Ok. your done

Do step 4.2 until above line for the nalalaingka.com.

Finish!!

Can’t login to mysql using web base apps

I experienced some weird things on MySQL. This was not only once but every time, i installed a new instance of MySQL Server.

The system i’m porting to the database can’t able to connect to the MySQL Server. So I tried some series of test to trouble shoot the problem.

First, i created a simple scripts that log-in to mySQL
Second, i Create a query to execute and display the table contents.

Boom!!!!
At my first step, i find out that i cant login to the database. So i used my friend google to find out whats is the prblem. And i came up to this site ‘http://dev.mysql.com/doc/refman/4.1/en/old-client.html’.

This is the simple solution:

1. Go to: MySQL Commandline Client
2. Login using your root password
3. mysql> set password for ‘root’@'localhost’ = OLD_PASSWORD(‘newpassword’);
4. If successful,
Query OK, 0 rows affected (0.00 sec)
mysql>
5. If not, try again

@echo off

One of my office mate (echo) was creating some batchfile when suddenly one of co-employee ask why his batchfile wouldnt work.

He told to echo that he is using his name as variable on batchfile just like echo is doing. And he demonstrated that he uses “@ryan off” but simply not working, while echo’s “@echo off” is working fine.

Of course we look at him grinning and explain that @echo is a command in DOS which by coincidence is echo is using.

When your echo is on, it will display each line of code on the screen but when your echo is off, it will only display the command output on the screen.

Copying files on the network

Since i already tackled some batch file from my recent post. I will share some tips on copying/backing-up files on the network eventhough the files was in-use. It frustrates us sometimes when we are copying files on the network then at the middle of copy process it will stop and says “Can’t copy files.files because the file was in-use”. Grrrr… you are copying a 30G of file then suddenly it will error like this.

So you want to copy files without interruption? Ok! we will formulate one. This DOS commands will copy all file on your source folder, but if you have already the file on your target folder it will not. But if the old file on your source folder was modified or was updated this dos command will copy the updated files eventhough it is already present in your target folder.

1. Map the source computer and the target computer. Let say we put the target computer to drive x:\ and source to y:\. Of course you can use your local computer as your target and source too.

source computer/folder == Y:
target computer/folder == X:

2. Now ceate the batch file. Open your text pad or notepad and SAVE AS to copy.bat. Note: before clicking the SAVE button, it must be sure that the SAVE AS TYPE == ALL FILES and the ENCODING == ANSI

3. then create the script

@echo off
xcopy “y:\source folder\*.*” x:\target folder /c /s /r /d /i /h /y

rem back up successful!
pause

4. Save the file.
5. Close the batch file. Locate the batch file, then double click. [It must open the Command line interface and excute the script]. When the execution was finish, you must see the “back up successful!”.

6. Explaination here

@echo off ##

xcopy ## it will copy all the files and subdirectories

*.* ## it will copy all file with any extension. You can also select file according to extension like *.xls or according to files itself like reports.*

/c ## continue to copy even there is an error

/s ## copy directories and subdirectories except the empty one

/r ## overwrites read-only files

/d ## date

/i ## If destination does not exist and copying more than one file, assumes that destination must be a directory.

/h ## Copies hidden and system files

/y ## overwrite existing files

If you will use this for back up purposes. Let say you will backup a target folder every wednesday, you can schedule your back up using Windows Scheduler. START>PROGRAM>ACCESSORIES>SYSTEM TOOLS>SCHEDULED TASKS create new schedule and map to the bat file.

Enabling IIS 7 on MS Vista home Edition

Got my New Toy and lil bit confused with the security of vista and its features. So as a so “Called Internet developer”, i look first how to install the IIS. Since i’m windows 2000 nuthugger , i’m looking for the so called “Windows Components” on vista. Hayyyy… It was already changed to Program and Features.

1. Go to your control panel
2. Select Programs and under the Program and Features, Turn Windows Features to ON/OFF
3. When all the Services features are all displayed, Choose Internet Information System. In IIS 7, you have to set all the services you want because when you install the IIS 7 by default, it will only install the default.

PHP5/MySQL trick on Windows platform

It’s not like the legacy setup on win32 platform, there are some tweaks to do before you can totally integrate MySQL to PHP5.

For example if you are using a Windows 2003 as your web server and you want to enable php5 server side scripting and MySQL database server.

1. Open your php.ini located on [Drive:\windows]

2. Uncomment [extension=php_mysql.dll]

3. Insert [extension_dir=drive:\php5\ext]

4. Copy [libmysql.dll] from [drive:\C:\Program Files\MySQL\MySQL Server 5.0\lib\debug] to [drive:\windows]

5. Finish

Thats all folks