jump to navigation

Connect PHP with Oracle database October 18, 2008

Posted by me2blog in PHP and Oracle database.
Tags: , , ,
7 comments

I spent the last four days looking for the correct way to Connect Apache server (PHP) with Oracle database, unfortunatly I didn’t find any complete article.So in this blog, I’m giving my expernce to connect ‘em 2geth

1. Install Apache server:

I make this step in a seprated blog page, plz check it from this link:Install Apache HTTP server

2. Extract the PHP zip folder:

1. Unzip the PHP file into c:\php

2. Copy all the files with the .dll extension to C:\WINDOS\system

3. Copy php5apache2.dll to C:\Apache\Apache2\modules


3. Copy the file php.ini-recommended and then edit it:

1. Open C:\php then copy the file php.ini-recommended to C:\WINDOWS

2. Rename it to be php.ini then open it with text editor

3. Set the doc_root to be: doc_root = c:\apache\apache2\htdocs

4. Set the extension_dir to: extension_dir = “C:\php\ext”

5. Uncomment (Remove the semicolon “;” from the beginning of the line) this line:

session.save_path = “c:/temp”

6. Uncomment the line: extension=php_oci8.dll

4.Edit the http.conf file:

1. Open the httpd file using any text editor, which exist on the path

C:\Apache\Apache2\conf

2. Find the line AllowOverride None and change the None value to be All

3. Look for the line DirectoryIndex index.html index.html.var and replace it with this line: DirectoryIndex index.html index.html.var index.php

4. Search for a section that has a lot of AddType commands, then added this line: AddType application/x-httpd-php .php so u have a total of 3 AddType command.

5. Search for a section has many Loads and add this line to it:

LoadModule php5_module modules/php5apache2.dll

5.Restart the Apache and the oracle database:

1. Restart the apache server using the cmd command (U can do it from the services but using the cmd has the advantage of showing errors while restarting the apache if there is any)

  • Open start -> run -> cmd -> cd c:\apache\apache2\bin (press enter)
  • The write apache –k restart (Press enter)

2. Restart the Oracle database

Open control panerl -> administrative tools -> services -> the restart all the services starting with Oracle

6. Test the connection:

1. To make the basic connection you need some information from the tnsnames.ora file, so follow the link C:\oracle\product\10.2.0\db_1\NETWORK\ADMIN Then open it

2. Open textpad page or use any text editor you want the past this code:

<?php

$db = “(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.34)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))” ;

if ($c=OCILogon(“system”, “your database password“, $db)) {

echo “Successfully connected to Oracle.\n”;

OCILogoff($c);

} else {

$err = OCIError();

echo “Connection failed.” . $err[text];

}

?>


Where the HOST is taken from your tnsnames.ora file and SID value is equal to your database name Then save the file with the name test.php

3. Open web browser page and type on the title: http://localhost/test.php

If it all worked well you will get white page with the result:

Successfully connected to Oracle.

NOTE: Recommended links to download the software’s:

Software

Version

URL

Oracle Database 10g Express Edition

10.2

http://www.oracle.com/technology/products/database/xe/

Apache HTTP Server

2.0.58

http://httpd.apache.org/download.cgi

PHP Hypertext Processor

5.1.3

http://www.php.net/downloads.php

Install Apache HTTP server: October 18, 2008

Posted by me2blog in PHP and Oracle database.
Tags: , ,
3 comments

To install the Apache server, it’s straight forward …

Just follow the pictures :)

checking the information from HTML forms October 1, 2008

Posted by me2blog in Common PHP Applications, php.
Tags: , , , , , , , , , , , ,
4 comments

b4 u use the values in your script, u need to check the variables to make sure they contain what u expect ‘em to contain! Thus never trust info from user.

Checking for empty fields:

u can require the user to enter info in a field and check when the user didn’t and let him back to re-fill that field using:

empty($_POST['field_name']);

this function returns true if the field is empty,,, false otherwise !

Checking for specific format:

u can check using some built in functions like if u expect string, u can check using is_string and so on… for a table of these functions c this previous post Changing the order of statement execution

Using regular expressions to check user input:

u can aslo compare the info to a pattern to c if it matches if u care a lot about the information pattern using ereg function … for more in this ,,, check this previous post: pattern matching with regular expressions

Cleaning information:

  • strip_tags: this function removes all tags from the text, u can keep some tags:

$variable_name = strip_tags($_POST)['field_name'],”<tag u allow> <tag u allow>“);

  • htmlspecialchars: this function changes some special characters to HTML into HTML format that allow ‘em to display without any special meaning

Examples: < become &alt;

? become &gt;

and & become &amp;

$variable_name= htmlspecialchars($_POST)['field_name']);

  • trim: remove extra spaces at the beginning and the end of the field info, bcz it’s familiar that user enter spaces by mistake.

$variable_name= trim($_POST)['field_name']);

Example:

in this example, i will use the same forms in the previous post but i will check for three fields (first name, second name and last name) not to keep blank ,,, if so … the user will be asked to re-fill ‘em …. if they r not blank , then it will output the same information in that example….

Building the forms code:

<br><center><h2> Customer information </h2></center>
<br><br>

<center>
<table border=”0″>
<form action=”customer_info.php” method=”POST”>
<tr> <td> First name:</td> <td> <input type=”text” name=”first_name”></td></tr>
<tr> <td>Second name: </td> <td> <input type=”text” name=”second_name”> </td></tr>
<tr><td>Last name: </td> <td> <input type=”text” name=”last_name”></td> </tr>
<tr><td> Street Address:</td><td> <input type=”text” name=”st_address”></td> </tr>
<tr><td> City:</td><td>
<select name=”city”
<option> Amman </option>
<option> Aqaba </option>
<option> Irbd </option>
<option> Zarqa </option>
</td> </tr>
<tr><td>Sex: </td><td>
<input type =”radio” name=”Radio1″ value= “M” > Male
<input type =”radio” name=”Radio1″ value= “F”> Female
</td></tr>
<tr><td>Your Operating system: </td><td>
<input type= “checkbox” name=”os0″ value=”W”> Windows
<input type= “checkbox” name=”os1″ value=”L”> Linux
<input type= “checkbox” name=”os2″ value=”M”> Mac
</td></tr>
</table><br>

<input type=”submit” value=”submit name”>
</form>

the customer info code:

<?php

function set_sex(){
if ($_POST[Radio1]==”M”) {
$sex= “Male”;
}
else
{
$sex= “Female”;}
}

function print_info()
{

echo “Welcome “,$_POST['first_name'],”!<br>”;
echo “Your second name is: “,$_POST['second_name'];
echo “<br>Your last name is: “,$_POST['last_name'];
echo “<br>”;
echo “you are “, $sex;
echo ” living in: “, $_POST[city];
echo “  in the “, $_POST[st_address],” Street”;
echo “<br>and your operating systems are: <br>”;

if (isset($_POST['os0'])) {
echo “Windows<br>”;
}

if (isset($_POST['os1'])) {
echo “Linux<br>”;
}

if (isset($_POST['os2'])) {
echo “Mac<br>”;
}
return;
}

function blank_check(){
if (empty($_POST['first_name']))
{
echo “You didn’t enter your first name! <br>”;
$blank_check_var = TRUE;
}

if (empty($_POST['second_name']))
{
echo “You didn’t enter your second name! <br>”;
$blank_check_var = TRUE;
}

if (empty($_POST['last_name']))
{
echo “You didn’t enter your last name! <br>”;
$blank_check_var = TRUE;
}

return $blank_check_var;

}

//main
set_sex();
$check_var= blank_check();
if (!$check_var){
print_info();
}

?>

The output after leaving the first name blank is:

You didn’t enter your first name!

The output after leaving the first and second name blank is:

You didn’t enter your first name!
You didn’t enter your second name!

The output after leaving the first, second and last name blank is:

You didn’t enter your first name!
You didn’t enter your second name!
You didn’t enter your last name!

Receiving the information from HTML forms October 1, 2008

Posted by me2blog in Common PHP Applications, php.
Tags: , , , , , ,
add a comment

The forms data r available at PHP built in array. As said earlier there two methods to send the data: POST and GET, so information from any form uses the method POST is available in the built-in array $_POST with the key for each field with it’s name! and information from any form uses the method GET is found in the array $_GET, and the key for each field is it’s name …

The information that user select from drop down menu or radio buttons r similarly available for use, but the situation is a lil different while using check boxes bcz the user can check more than one box, the information is in multidimentional array ,,,, but i think there is an easier wayL when u create check box bottons, name each buton a unique name, and get it from 1D array, it will be faster and easier to code … remember no one can c ur code ,,, every one will c the effect of ur code, so write it in the easiest way 4 u :p ;)

What is the difference between POST and GET:

  • Get method: The form data is passed by adding it to the URL. The advantages for this method: it’s simple and fast but the disadvantages r less data to be passed and less secure (since the information r displayed in the browser).
  • Post method: The form data is passed as a package in a separate communication with the processing script. The advantages: unlimited information to be passed and more secure and the disadvantage is the overhead which slows the speed.

Example:

<br><center><h2> Customer information </h2></center>
<br><br>

<center>
<table border=”0″>
<form action=”customer_info.php” method=”POST”>
<tr> <td> First name:</td> <td> <input type=”text” name=”first_name” value=”enter ur first name plz”></td></tr>
<tr> <td>Second name: </td> <td> <input type=”text” name=”second_name” value=”enter ur second name plz”> </td></tr>
<tr><td>Last name: </td> <td> <input type=”text” name=”last_name” value=”enter ur last name plz”></td> </tr>
<tr><td> Street Address:</td><td> <input type=”text” name=”st_address” value=”enter ur st address plz”></td> </tr>
<tr><td> City:</td><td>
<select name=”city”
<option> Amman </option>
<option> Aqaba </option>
<option> Irbd </option>
<option> Zarqa </option>
</td> </tr>
<tr><td>Sex: </td><td>
<input type =”radio” name=”Radio1″ value= “M” > Male
<input type =”radio” name=”Radio1″ value= “F”> Female
</td></tr>
<tr><td>Your Operating system: </td><td>
<input type= “checkbox” name=”os0″ value=”W”> Windows
<input type= “checkbox” name=”os1″ value=”L”> Linux
<input type= “checkbox” name=”os2″ value=”M”> Mac
</td></tr>
</table><br>

<input type=”submit” value=”submit name”>
</form>

and now …. the customer_info.php code:

<?php

if ($_POST[Radio1]==”M”) {
$sex= “Male”;
}
else
{
$sex= “Female”;}

echo “Welcome “,$_POST['first_name'],”!<br>”;
echo “Your second name is: “,$_POST['second_name'];
echo “<br>Your last name is: “,$_POST['last_name'];
echo “<br>”;
echo “you are “, $sex;
echo ” living in: “, $_POST[city];
echo “  in the “, $_POST[st_address],” Street”;
echo “<br>and your operating systems are: <br>”;

if (isset($_POST['os0'])) {
echo “Windows<br>”;
}

if (isset($_POST['os1'])) {
echo “Linux<br>”;
}

if (isset($_POST['os2'])) {
echo “Mac<br>”;
}

?>

The output:

and after clicking the submit name button u will c the following page:

Welcome Noor!
Your second name is: A
Your last name is: Adiga
you are Female living in: Aqaba in the Abc ST Street
and your operating systems are:
Windows

working with HTML forms September 29, 2008

Posted by me2blog in Common PHP Applications, HTML, php.
Tags: , , ,
add a comment

For a web page tp be active, it must collect information from the user. which is done using HTML forms.

Displaying HTML forms:

there is two ways to do that, u can write the HTML code directly or inside the echo statement, which gives the browser html sentences.

<form action = “page to open after click submit.php” method= “POST” >

<input type= “text” name= “field name” value = “default value to display“>

<input type= “text” name= “field name” value = “default value to display“>

<input type =”submit” value = “Submit button name“>

  • action: u write here the name of the page u want the user to go into after clicking the submit button
  • method: there r two ways of methods: POST and GET … i will talk about ‘em later in this blog
  • now, we start creating the fields using <input type=”text” which means that the field will be text field
  • name: the name of the field, will not be displayed, just to work with while taking information from it
  • value: this value will be written inside the field box at the beginning
  • now, to make the submit button, by putting the type = “submit”
  • value: this will be the text written over the button

Adding selection list:

<select name=”the list name“>

<option> “first option” </option>

<option> “second option” </option>

<option> “fthird option” </option>

</select>

Adding Radio Buttons:

<input type=”radio” name = “radio  name” value = “the value to be stored while receiving the info “> “text to display “

<input type=”radio” name = “radio  name” value = “the value to be stored while receiving the info “> “text to display “

Its important to use the same name for all radio options. otherwise it won’t work probably!

Adding check boxes:

<input type=”checkbox” name=”check box name” value= “value to be stored” > “text to display

<input type=”checkbox” name=”check box name” value= “value to be stored” > “text to display

Example:

<br><center><h2> Customer information </h2></center>
<br><br>

<center>
<table border=”0″>
<form action=”customer_info.php” method=”POST”>
<tr> <td> First name:</td> <td> <input type=”text” name=”first_name” value=”enter ur first name plz”></td></tr>
<tr> <td>Second name: </td> <td> <input type=”text” name=”second_name” value=”enter ur second name plz”> </td></tr>
<tr><td>Last name: </td> <td> <input type=”text” name=”last_name” value=”enter ur last name plz”></td> </tr>
<tr><td> Street Address:</td><td> <input type=”text” name=”st_address” value=”enter ur st address plz”></td> </tr>
<tr><td> City:</td><td>
<select name=”city”
<option> Amman </option>
<option> Aqaba </option>
<option> Irbd </option>
<option> Zarqa </option>
</td> </tr>
<tr><td>Sex: </td><td>
<input type =”radio” name=”Radio1″ value= “M” > Male
<input type =”radio” name=”Radio1″ value= “F”> Female
</td></tr>
<tr><td>Your Operating system: </td><td>
<input type= “checkbox” name= os value=”Windows1″> Windows
<input type= “checkbox” name= os value=”Linux1″> Linux
<input type= “checkbox” name= os value=”Mac1″> Mac
</td></tr>
</table><br>

<input type=”submit” value=”submit name”>
</form>
The output: