Web Development

Templating in PHP

So you want to have a template system for your project but don’t know how. Let us teach you. First of all you need to decide on the template(container)

For example a template maybe something like:

 

Code:

<html>
<head>
<!--Some code-->
<title>{{page_title}}</title>
<LINK href="{{css}}" rel="stylesheet" type="text/css">
</head>
<body>
<!--Menu-->
<a href="home.php">Home</a>||<a href="links.php">Links</a>
<br>
{{body}}
</body>
</html>

Here it is clear what all we need to do…
A function will replace {{page_title}} with the supplied page title. {{css}} with supplied css address and the {{body}} with the supplied page body.

We define page_draw() like this:

 

PHP Code:

function page_draw($body, $title="Default title",$css="default.css",$templatefile="template.php")

Now here we are accepting default values for title and css and will require values of body to be supplied. This eliminates the need of frames(which suck anyways)
The definition of the function will be something like:

 

PHP Code:

function page_draw($body,$title="Default title",$css="default.css", $templatefile="template.php")
{
$fh = fopen($templatefile, 'r');
$data = fread($fh, filesize($templatefile));
//Now we need to replace the {{css}} with the file and {{body}}etc.
$tags=array("{{page_title}}","{{css}}","{{body}}");
$rplc=array($title,$css,$body);
$str=str_replace($tags,$rplc,$data);
echo
$data;
}
//Done with the function

We store this in a file called say “functions.php”
So we have created the function
Now about using it….
Say we have to use it in index.php
So we include the file functions.php and write the body for it and we go

 

PHP Code:

<?include_once "functions.php";
$body="Hello Everyone Welcome to my homepage<br>All audience will be entertained.<br>Check out ddboard.com for free domains";
$ttl="Welcome to my website: Homepage";
$css="cssfile.css";
page_draw($body,$tts,$css);
?>

You are done. The page will show a menu apart form the text you have entered here.
This is specially useful if you have lots of pages or your content changes too fast;
You can even download the menu from another file.
Such functions are the base of most cms, forum etc scripts…….
Please suggest any corrections

Avoid using javascript for form validation

Okay not exactly avoid but use a server side language to validate as well….
Here’s what i mean.

You have a form with the following fields, say
Name:
Age:
email:

And you write a javascript that runs when submit is clicked so as to check that email is a valid email address. So far so good….
When the user enters an invalid email address, you alert him and the form is not submitted.
BUT as many of us know, many browsers allow the user to disable javascript.
Under such circumstances, the form will not be checked and an invalid email address may be entered…..

So it is always advised to cross check the form data entered in PHP or ASP etc for valid data.

Because it is next to impossible to stop PHP on the client side…..

POST or GET which method to use

Okay so u have a form and a preprocessor(i mean PHP). Now you’re wondering what method to use to process the form….

GET or POST
First of all let’s see the difference. In GET, the contents of the Form are sent in the url
eg. If you are using a file process.php which accepts the fields name, email, class, etc and does something with them. Then on submitting the form, the browser will open this URL:
http://someurl.com/process.php?name=Somename&email=someemail& class=XYZ

but in case of post, it sends an intrinsic array, which is not visible to the user(like get is).

So you should use Post when either the data is too complicated…Imagine a 3 line paragraph in your browser url
or you need security….
Why POST is secure?
Obviously because the information is sent intrinsically. However this doesn’t mean that it is secure from pro-hackers etc. Its secure compared to GET…..
Another reason is that you don’t quite require a Form to be filled up when using get
A simple img tag is enough to send the data
In the previous case, the script can be provoked using
<img src=”http://someurl.com/process.php?name=Somename&email=someemail& class=XYZ”/>
This will not show any image but will do the job anyways. But in post this is not possible,
The problem with get becomes significant if you have a big thing

Suppose the file send message is used by the user to send message a friend(in a social networking environment)
it works with friend id and message as parameter(http://somesite.com/sendmessage.php?friendid=120&message=hi)
but now an unscrupulous site puts up an image on its site
<img src=”http://somesite.com/sendmessage.php?friendid=221344&message=You%20bloo dy%20bitch%20Get%20Lost>
The user while logged on into ur site also visits this site.
He will unintentionally send a message to this user….
so this is where u need post..

Should you always use post….?
No not quite. Depends on you requirement. If the action performed is something that doesn’t make much of a difference in the site or user’s life,
go on
A profile page will have to use get
http://someurl.com/profile.php?user=username
Otherwise users cannot give out their profile links….
or even a refferal url(http://someurl.com/?ref=idsadas)
which is again given out by people

Processing Forms

Okay today, i will teach you how to process forms using PHP.

First of all let’s consider and average form:

<form action=”process.php” method=”GET”>

Name: <input type=”text” name=”name”/>

Age: <input type=”text” name=”age”/>

<input type=”submit value=”Submit Form”/>

</form>

Name:

Age:

So, here, the form is processed by a file named process.php. The form method decides how the date is sent to the process.php. In the case of GET, the data is sent postfixed to the filename process.php. eg:if the data entered in name and age are John and 18 respectively, the browser will open a page with the URL. http://yoursite.com/process.php?name=John&age=18

Now in the process.php you will need to accept this data and do something with this. To receive the data, you simply need to use the $_GET or $_POST array depending on the request method used. If you don’t want to be specific of the method, use $_REQUEST. The name of the form element(<input type=text name=”…”>) decides the value to be entered inside the [' and ']

So the code looks something like
<? $name=$_GET['name'];
$years=$_GET['age'];
echo "The name was".$name." and the age was".$years;
?>

Additionally you should use the PHP’s functions to process the form(send email, put into a database,etc)

A short tutorial on PHP

A very basic tutorial on PHP. You can’t do much with this….
Please understand that this is a very basic tutorial. For more advanced stuff, please use google to search(Provided here too)


Okay so here we go….PHP is a preprocessor. That is files are processed by a program running on server before the output is sent to the browser requesting for it. Only the code within <? and ?> is processed, nothing else.You can put html and php code in the same page.eg:<body>Welcome to my website.<?echo “hello world”?>

</body>

Alright so that’s the first piece of code….echo is used to type a certain text to the browser. The whole tag : echo “hello world” is replaced with: hello world. This is the very basic and the most important command(function rather) in PHP.

Read the rest of this entry »