Advertise here.
Showing posts with label Web Development.. Show all posts
Showing posts with label Web Development.. Show all posts

Tuesday, 28 August 2012

ASP Lesson #5 - Using a Database


Creating an Access database

First we will create a database in Access called Employees.mdb. This will have a table called Employee and here is what it should look like:

Employee
EmpNo
FirstName
LastName

Insert the following values into the tables:

Employee
1JohnSmith
2MaryJones
3JamesKing


The Connection object

The first thing to do before you can use a database is create a Connection object.

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")

Next set the ConnectionString property of the Connection and open the Connection.

cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & Server.MapPath("Employees.mdb")
cn.Open

The RecordSet object

We now need a RecordSet object to store the results of a query. You must create the RecordSet in a way similar to how you created a Connection.

Dim rs
Set rs = Server.CreateObject("ADODB.RecordSet")

Before you can go on with the next step you need to copy C:\Program Files\Common Files\System\ado\adovbs.inc to C:\Inetpub\wwwroot. Also add the following line after <%Option Explicit%>:

<!--#include file="adovbs.inc"-->

Now we can open the RecordSet like this:

rs.Open "SELECT * FROM Employee", cn, adOpenStatic, adLockOptimistic

The first parameter in quotes after rs.Open is a SQL statement that says all records from the Employee table must be retrieved. The second parameter is the connection. The Third parameter is the CursorType and the Fourth parameter is the LockType.

Reading from a RecordSet

Now that we have opened the RecordSet we can read values from it. We will use a loop to write the first name of each employee in the Employee table on the page.

While Not rs.EOF
   Response.Write rs("FirstName") & "<br>"
   rs.MoveNext
Wend

The Find command can be used to find a specific record.

rs.Find "LastName = 'King'"

Writing to a RecordSet

You can change the value of a field in the RecordSet by setting its value in the same way as you set the value of a variable. After you have set the value you have to use the Update command or the changes will not happen.

rs("FirstName") = "Mike"
rs.Update

Cleaning up

You must always close all Connections and RecordSets when you are finished using them. You must also set them equal to Nothing. If you do not then they will stay in the server's memory until it has not memory left.

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

ASP Lesson #4 - Session Variables and Cookies


The variables that we have created so far only exist on the page that they are declared. Session variables are the same as normal variables except that they can be used by other pages. Session variables only exist for a certain amount of time which is 10 minutes by default.

Using session variables

You must use the Session object to read and write session variables. Here is an example of how to set the value of a new session variable on one page and how to use it on another page:
page1.asp

Session("MySessionVariable") = Request("txtName")

page2.asp

Response.Write Session("MySessionVariable")

What is a cookie?

Cookies are similar to session variables because they store data that can be used by other pages. The difference is that cookies are stored on the client's computer and session variables are stored on the server. Cookies also last a lot longer that session variables.

Using cookies

The Response.Cookies object is used to create and set the value of a cookie.

Response.Cookies("Name") = "John Smith"

The Request.Cookies object is used to read the value of a cookie.

Response.Write Request.Cookies("Name")

You can also group values under another name such as having first name and last name in the name group.

Response.Cookies("Name")("FirstName") = "John"
Response.Cookies("Name")("LastName") = "Smith"

You can set how long a cookie will be kept on the client's computer before it expires using the 
Expires property of the cookie. You must add the amount of days you want it to last to the current date when you do this.

Response.Cookies("Name") = "John Smith"
Response.Cookies("Name").Expires = Date + 30

ASP Lesson #3 - Control Structures


If Statement

You can make a decision based on user input using an If statement. The If statement tests a condition and if the result is true then it runs the code that follows the If statement but if the result is false then it runs the code after the else statement. Here is an example for when someone enters their age on a form:

Dim Age
Age = Request("txtAge")
if Age >= 18 then
   Response.Write "You are old enough to use this site"
else
   Response.Write "You are too young to use this site"
end if

Select Case Statement

The Select Case statement is similar to the If statement because it also used for making decisions. The difference is that the Select Case statement can have many conditions and the code to run for when the result of each condition is true. Here is an example of choosing a country from a dropdown listbox:

Dim Country
Country = Request("lstCountry")
Select Case Location
Case "USA"
   Response.Write "You are from the USA"
Case "UK"
   Response.Write "You are from the UK"
Case "SA"
   Response.Write "You are from SA"
End Select

For Loop

The For loop is used to loop from one number to another. The code inside the loop is repeated until the second number is reached. Here is an example of counting to 10 using a For loop:

Dim i
for i = 1 to 10
   Response.Write i
next

While Loop

The While loop checks if a condition is true before each time that the code inside the loop is repeated. The loop variable must be set before it is used in a While loop and the loop variable must be incremented inside the loop.
Dim j
j = 1
While j <= 10
   Response.Write j
   j = j + 1
Wend

Do Loop

The Do loop is the same as the While loop except that the condition is tested at the bottom instead of at the top of the loop.

dim k
k = 1
do
   Response.Write k
   k = k + 1
loop until k > 10

ASP Lesson #2 - Forms


An HTML form is a group of text boxes and buttons other things that allow a user to enter data. Here is the form that you see quite often which we will be using:

Username: 
Password: 
Here is the HTML code for the form:

<form method="get" action="showdetails.asp">
Username: <input type="text" name="txtUsername"></input>
Password: <input type="text" name="txtPassword"></input>
<input type="submit" value="Login"></input>
</form>

The method="get" will show the querystring as part of the address in your web browser. The querystring is all the things that come after the ? in an address. Use method="post" to hide the querystring. action=showdetails.asp means that when the Login button is clicked it will go to showdetails.asp where we will process the data from the form.

You can save this form along with all the other code that you need for a basic HTML page as login.htm. The page with the form doesn't have to be an asp page because it doesn't process anything. It is the showdetails.asp page that does all the processing.

Now we will create showdetails.asp. First type the basic code required for an HTML page and then the code that is required for an ASP page. Here is an example:

<% @Language = VBScript %>
<% Option Explicit %>
<html>

<head>
<title>My First ASP Page</title>
</head>

<body>
<%
%>
</body>

</html>

Make sure you save both pages in C:\Inetpub\wwwroot

Requesting form field values

The Request command is used to get the values from the fields of the form. You should first declare variables to store the values that you request. Use the Dim command to declare the variables like this:

Dim Username, Password

If you know Visual Basic then you must remember that you can't choose what data type you want to declare a variable as like you can is Visual Basic.

Next you store the value from the form in the variable using the Request command like this:

Username = Request("txtUsername")
Password = Request("txtPassword")

We must also write the values on the page to make sure it worked using Response.Write.

Response.Write "Your Username is " & Username & "<br>"
Response.Write "Your Password is " & Password

You will see that the & operator has been used to join the string "Your Username is" and the variable Username. You will also see that you can write HTML tags and that they must be between quotes when you it.

Try open http://localhost/login.htm and type something in the form fields and click the login button. Have a look at the result and make sure you understand what we have done.

Functions

There are functions such as the Date function which are built into ASP. Here is an example of how to write the date using the Date function:

Response.Write "The date is " & Date

There are also functions that you can use on data. The Left function for example can be used to get the first letter of the name a person entered.

Dim Name, Surname, Initial
Name = "John"
Surname = "Smith"
Initial = Left(Name, 1)
Response.Write "Hello Mr " & Initial & " " & Surname

There are many more functions available and you can find them on w3schools' list of VBScript functions.

ASP Lesson #1 - Introduction to ASP


This tutorial will teach you how to use ASP(Active Server Pages). It only includes the most important things that you need to know about ASP and leaves out the things that are not used very often. This means that you will learn ASP quicker and not have to go through boring things that you might never use.

Requirements

If you are using Windows 95 or NT 4.0 you need to download and install the Windows NT 4.0 Option Pack.

If you are using Windows 98 then you need to install Personal Webserver from your Windows 98 CD.

If you are using Windows 2000 or XP then you need to install IIS(Internet Information Services) from Control Panel->Add or Remove Programs->Add/Remove Windows Components.

If you don't have any of those then you can get yourself an ASP webhosting account but it is a lot more difficult to learn doing it this way.

You need Microsoft Access if you want to learn about using a database with ASP and you need to know how to use Access.

Other things that you are not required to know but that will help you are:
  • HTML Forms
  • Visual Basic
  • SQL

First ASP Page

We will be creating our ASP pages using Notepad. Open Notepad and type the following code for a basic html page:

<html>

<head>
<title>My First ASP Page</title>
</head>

<body>
</body>

</html>

Now type the following before all the HTML code:

<% @Language = VBScript %>

This tells the webserver that we will be using VBScript as the scripting language in our ASP page. Next you must add the following line straight after the one you have just typed:

<% Option Explicit %>

This tells the webserver that all variables must be declared before they can be used. We will learn about variables later. It is important that you put those 2 lines on all your ASP pages.

All ASP code must be put between an opening <% and a closing %> tag. Your main ASP code should go between the <body> tags.

We will now write the words "Hello World" on our ASP page. The Response.Write command is used to write things. Type the following between the ASP tags:

Response.Write "Hello World"

You can write comments that are ignored by the webserver by typing them after a '. You can write a comment which tells you what the Response.Write to see how it is done. Here is what you should type:

' Writes the words Hello World

Here is what you should have so far:

<% @Language = VBScript %>
<% Option Explicit %>
<html>

<head>
<title>My First ASP Page</title>
</head>

<body>
<%
' Writes the words Hello World
Response.Write "Hello World"
%>
</body>

</html>

Another way to write things is to use just an = in front of what you want to write but you can only do it this way if it is put inside its own set of ASP tags. Here is an example:

<% ="Hello World" %>

You must now save your ASP page. Save it as first.asp in C:\Inetpub\wwwroot. Make sure you choose All Files for the Save as Type. If you don't have a C:\Inetpub\wwwroot folder then you have not installed PWS or IIS. Now open your web browser and go to the http://localhost/first.asp. If you have done everything right then you will see a page that says 
Hello World. Congratulations you have just made your first ASP page.

If you do a View Source on the page that is displayed in your browser you will see that the ASP tags have disappeared. This is because everything between the ASP tags are processed before it is sent to the browser. It is important to understand that the file that the browser receives it not the same ASP file that you created. Don't try editing your ASP file by doing a View Source because it won't work.

Friday, 24 August 2012

PHP Tutorial Lesson #12 Forms

Form Handling


The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.


Example


The example below contains an HTML form with two input fields and a submit button:


<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>    


When a user fills out the form above and click on the submit button, the form data is sent to a PHP file, called "welcome.php".

"welcome.php" looks like this:


<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html> 


Output could be something like this:


Welcome John!
You are 28 years old.  

The PHP $_GET and $_POST variables will be explained in the next sections.



Form Validation


User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and reduces the server load.

You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.



The $_GET Variable


In Php, the predefined $_GET variable is used to collect values in a form with method="get"

Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.


Example


<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>


When the user clicks the "Submit" button, the URL sent to the server could look something like this:

http://www.w3schools.com/welcome.php?fname=Peter&age=37

The "welcome.php" file can now use the $_GET variable to collect form data (the names of the form fields will automatically be the keys in the $_GET array):

Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!


When to use method="get"?


When using method="get" in HTML forms, all variable names and values are displayed in the URL.

Note: This method should not be used when sending passwords or other sensitive information!

However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.





The $_POST Variable


The predefined $_POST variable is used to collect values from a form sent with method="post".

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).


Example



<form action="welcome.php" method="post">

Name: <input type="text" name="fname" />

Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL will look like this:

http://www.mywebsite.com/welcome.php

The "welcome.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.


When to use method="post"?

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

The PHP $_REQUEST Variable

The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.

Example

Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.





PHP Tutorial Lesson #11 Functions


In this chapter we will show you how to create your own functions.

To keep the script from being executed when the page loads, you can put it into a function.

A function will be executed by a call to the function.

You may call a function from anywhere within a page.


Create a PHP Function


A function will be executed by a call to the function.


Syntax



function functionName()
{
    code to be executed;
}


PHP function guidelines:


  • Give the function a name that reflects what the function does
  • The function name can start with a letter or underscore (not a number)



Example


A simple function that writes my name when it is called:


<html>
<body>

<?php
function writeName()
{
    echo "Mohammed Kateregga";
}

echo "My name is ";
writeName();
?>

</body>
</html>




Output :

My name is Mohammed Kateregga



Adding parameters


To add more functionality to a function, we can add parameters. A parameter is just like a variable.

Parameters are specified after the function name, inside the parentheses.


Example 1


The following example will write different first names, but equal last name:


<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Kateregga.<br />";
}

echo "My name is ";
writeName("Mohammed");
echo "My sister's name is ";
writeName("Halma");
echo "My brother's name is ";
writeName("Abdulhameed");
?>

</body>
</html>

Output :

My name is Mohammed Kateregga.
My sister's name is Halma Kateregga.
My brother's name is Abdulhameed Kateregga.



Example 2




The following function has two parameters:

<html>
<body>
<?php
function writeName($fname,$punctuation)
{
echo $fname . " Kateregga" . $punctuation . "<br />";
}
echo "My name is ";
writeName("Mohammed",".");
echo "My sister's name is ";
writeName("Halma","!");
echo "My brother's name is ";
writeName("Abdulhameed","?");
?>
</body>
</html>

Output:

My name is Mohammed Kateregga.
My sister's name is Halma Kateregga!
My brother's name is Abdulhameed Kateregga?

Return values

To let a function return a value, use the return statement.

Example


<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>

Output:

1 + 16 = 17








PHP Tutorial Lesson #10 Looping


PHP Loops

Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
In PHP, we have the following looping statements:
  • while - loops through a block of code while a specified condition is true
  • do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

The while Loop

The while loop executes a block of code while a condition is true.

Syntax

while (condition)
  {
  code to be executed;
  }

Example

The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>

<?php
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }
?>

</body>
</html>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5


The do...while Statement

The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.

Syntax

do
  {
  code to be executed;
  
}
while (condition);

Example

The example below defines a loop that starts with i=1. It will then increment i with 1, and write some output. Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:

<html>
<body>

<?php
$i=1;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<=5);
?>

</body>
</html>

Output:

The number is 2
The number is 3
The number is 4
The number is 5
The number is 6

The for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (init; condition; increment)
  {
  code to be executed;
  }

Parameters:
  • init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
  • condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)
Note: Each of the parameters above can be empty, or have multiple expressions (separated by commas).

Example

The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>

<?php
for ($i=1; $i<=5; $i++)
  {
  echo "The number is " . $i . "<br />";
  }
?>

</body>
</html>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5


The foreach Loop

The foreach loop is used to loop through arrays.

Syntax

foreach ($array as $value)
  {
  code to be executed;
  }

For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.

Example

The following example demonstrates a loop that will print the values of the given array:

<html>
<body>

<?php
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "<br />";
  }
?>

</body>
</html>

Output:

one
two
three

PHP Tutorial Lesson #9 Arrays


Numeric Arrays


A numeric array stores each array element with a numeric index.

There are two methods to create a numeric array.

1. In the following example the index are automatically assigned (the index starts at 0):

$cars=array("Saab","Volvo","BMW","Toyota");

2. In the following example we assign the index manually:

$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";


Example


In the following example you access the variable values by referring to the array name and index:

<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota"; 
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>

The code above will output:

Saab and Volvo are Swedish cars.



Associative Arrays

An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.

Example 1

In this example we use an array to assign ages to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

Example 2

This example is the same as example 1, but shows a different way of creating the array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
The ID keys can be used in a script:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";
?>
The code above will output:
Peter is 32 years old.


Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
  (
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );
The array above would look like this if written to the output:
Array
(
[Griffin] => Array
  (
  [0] => Peter
  [1] => Lois
  [2] => Megan
  )
[Quagmire] => Array
  (
  [0] => Glenn
  )
[Brown] => Array
  (
  [0] => Cleveland
  [1] => Loretta
  [2] => Junior
  )
)

Example 2

Lets try displaying a single value from the array above:
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
The code above will output:
Is Megan a part of the Griffin family?


Complete PHP Array Reference

For a complete reference of all array functions, go to the complete PHP Array Reference.
The reference contains a brief description, and examples of use, for each function!

PHP Tutorial Lesson #8 Php Switch Statement


In some earlier code, we tested a single variable that came from a drop-down list. A different picture was displayed on screen, depending on the value inside of the variable. A long list of if and else … if statements were used. A better option, if you have only one variable to test, is to use something called a switch statement. To see how switch statements work, study the following code:



<?php

$picture ='church';

switch ($picture) {
case 'kitten':
print('Kitten Picture');
break;

case 'church':
print('Church Picture');
break;
}

?>

In the code above, we place the direct text "church" into the variable called $picture. It's this direct text that we want to check. We want to know what is inside of the variable, so that we can display the correct picture.

To test a single variable with a Switch Statement, the following syntax is used:

switch ($variable_name) {
case 'What_you_want_to_check_for':
//code here
break;
}

It looks a bit complex, so we'll break it down.

switch ($variable_name) {
You Start with the word 'Switch' then a pair of round brackets. Inside of the round brackets, you type the name of the variable you want to check. After the round brackets, you need a left curly bracket.

case 'What_you_want_to_check_for':
The word 'case' is used before each value you want to check for. In our code, a list of values was coming from a drop-down list. These value were: church and kitten, among others. These are the values we need after the word 'case'. After the the text or variable you want to check for, a colon is needed ( : ).

//code here
After the semi colon on the 'case' line, you type the code you want to execute. Needless to say, you'll get an error if you miss out any semi-colons at the end of your lines of code!

break;
You need to tell PHP to "Break out" of the switch statement. If you don't, PHP will simply drop down to the next case and check that. Use the word 'break' to get out of the Switch statement.



If you look at the last few lines of the Switch Statement in this file, you'll see something else you can add to your own code:

default:
print ("No Image Selected");

The default option is like the else from if … else. It's used when there could be other, unknown, options. A sort of "catch all" option.

PHP Tutorial Lesson #7 The If Statement


The if Statement


Use the if statement to execute some code only if a specified condition is true.

Syntax

if (condition) code to be executed if condition is true;

The following example will output "Have a nice weekend!" if the current day is Friday:

<html>
<body>

<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>

</body>
</html>

Notice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true.



The if...else Statement


You saw in the previous section that variables are storage areas for your text and numbers. But the reason you are storing this information is so that you can do something with them. If you have stored a username in a variable, for example, you'll then need to check if this is a valid username. To help you do the checking, something called Conditional Logic comes in very handy indeed. In this section, we'll take a look at just what Conditional Logic is. In the next section, we'll do some practical work.



Conditional Logic

Conditional Logic is all about asking "What happens IF ... ". When you press a button labelled "Don't Press this Button - Under any circumstance!" you are using Conditional Logic. You are asking, "Well, what happens IF I do press the button?"

You use Conditional Logic in your daily life all the time:

"If I turn the volume up on my stereo, will the neighbours be pleased?"
"If I spend all my money on a new pair of shoes, will it make me happy?"
"If I study this course, will it improve my web site?"

Conditional Logic uses the "IF" word a lot. For the most part, you use Conditional Logic to test what is inside of a variable. You can then makes decisions based on what is inside of the variable. As an example, think about the username again. You might have a variable like this:

$User_Name = "My_Regular_Visitor";

The text "My_Regular_Visitor" will then be stored inside of the variable called $User_Name. You would use some Conditional Logic to test whether or not the variable $User_Name really does contain one of your regular visitors. You want to ask:

"IF $User_Name is authentic, then let $User_Name have access to the site."

In PHP, you use the "IF" word like this:

if ($User_Name = = "authentic") {
//Code to let user access the site here;
}

Without any checking, the if statement looks like this:

if ( ) {

}

You can see it more clearly, here. To test a variable or condition, you start with the word "if". You then have a pair of round brackets. You also need some more brackets - curly ones. These are just to the right of the letter "P" on your keyboard (Well, a UK keyboard, anyway). You need the left curly bracket first { and then the right curly bracket } at the end of your if statement. Get them the wrong way round, and PHP refuses to work. This will get you an error:

if ($User_Name = = "authentic") }
//Code to Let user access the site here;
{

And so will this:

if ($User_Name = = "authentic") {
//Code to Let user access the site here;
{

The first one has the curly brackets the wrong way round (should be left then right), while the second one has two left curly brackets.

In between the two round brackets, you type the condition you want to test. In the example above, we're testing to see whether the variable called $User_Name has a value of "authentic":

($User_Name = = "authentic")

Again, you'll get an error if you don't get your round brackets right! So the syntax for the if statement is this:

if (Condition_or_Variable_to_test) {
//your code here;
}

In the next lesson, we'll use if statements to display an image on the page.

We'll use the print statement to "print out" HTML code. As an example, take the following HTML code to display an image:

<IMG SRC =church.jpg>

Just plain HTML. But you can put that code inside of the print statement:

print ("<IMG SRC =images/church.jpg>");

When you run the code, the image should display. Of course, you'll need an image called church.jpg, and in a folder called images.