You are on page 1of 18

PHP

Hypertext Preprocessor

DHTML
Using Array:‫ا لمصفوفات‬
 PHP supports two kinds of array:
1. Indexed‫ ف هرس‬: which use numbers as the key, as follow:

‫ترتيب في شكل‬

‫ارقام‬

2. Associative‫ ت رابطي‬: which use strings as a key, as follow:

‫ترتيب في شكل‬

‫نصوص‬
Some of Array methods:
method Description example
count() , sizeof() determine the number of $num=count($arr);
elements in an array
range() used to create an array of $alpha=range(‘a’,’z’);
sequential letters
sort() sorts an array by values sort ($arr);
discarding the original key,

 PHP supports foreach loop to access the elements


of array; as follow;
Syntax: foreach ($arr_name as $value)
{ echo($value); }
ex(1): using array- indexed array
<?php
# display.php
//using indexed array
$days = array (1=> 'Saturday', 'Sunday',
'Monday‘, 'Tuesday' ,'Wednesday',
'Thursday' , 'Friday’ );
http://localhost/display.php
//Display all days store on array
echo(" The days of the week are : <ul>"); The days of the week are:
foreach( $days as $value) • Saturday
{ • Sunday
echo(" <li> $value </li> ");
• Monday
}
• Tuesday
• Wednesday
echo ("</ul>");
• Thursday

?> • Friday
ex(2): using array- Associative array
<?php
# display.php
//using Associative array
$day[‘sat’] = 'Saturday';
$day[‘mon’] = ‘Monday';
$day[‘wen’] = ‘Wednesday';

//Display all days store on array http://localhost/display.php


echo(" My Favorite days are : <ol>");
My favorite days are:
foreach( $day as $key)
{
1- Saturday
echo(" <li> $key </li> ");
}
2- Monday
3- Wednesday
echo ("</ol>");

?>
ex(3): using array- range & count methods
<?php
# year.php //define values
$year = range (‘2000’,’2020’);
echo (“Array elements are :”);
foreach ( $year as $key)
echo(“<br />”. $key);

echo (“<br />The number of elements on array =“.count ($year) );

?> http://localhost/year.php
Array elements are:
2000
2001
2002
.....
2019
2020
The number of element= 21
Global-request-related automatic variables:
 $_GET : is an array of variable names and values sent
by the HTTP GET method.
GET_$ ‫ عبارة متغيرات من نوع‬
 $_POST: is an array of variable names and values sent
by the HTTP POST method.
POST_$ ‫ عبارة متغيرات من نوع‬
 $_REQUEST: works for both GET and POST method.
‫ ) معا‬GET and POST(‫عبارة عن دالة يستخدم‬
 $_COOKIE: is an array Stores cookie data.
‫ في شكل مصفوفة‬cookie ‫يتم تخزين البيانات‬
 $_SESSION: is an array Stores session data.
‫ في شكل مصفوفة‬session ‫يتم تخزين البيانات‬
PHP From processing:
 $_GET and $_POST variables are used to retrieve
information from forms.‫ي ستخدم ال رجاع ا لمعلوماتمنا لنموذج‬

 Information sent with GET method is visible to everyone


(displayed in the address bar), and it has limits on the amount of
information to send (max.100 characters).‫ي تم ارسا لا لمعلوماتمرئية‬

 Information sent with POST method is invisible to others and has no


limits on the amount of information to send. ‫ي تم ارسا لا لمعلوماتغير مرئية‬

 $_REQUEST variable used to get the form’s data that sent with both the GET and POST methods.

‫ ) معافيارسال البيانات‬GET and POST(‫عبارة عن دالة يستخدم‬


ex : using Post method ‫استخدام‬

Suppose we have a following form:

Form.html

Login Form

Username:

Password:

Submit
ex : using Post method
<!-- Form.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Using post variables - Login Form </title>
<body>
<h3> Login Form </h3>
<form action = “welcome.php" method= "post“ >
<label> Name : </label>
<input type= "text" name="username"> <br/>
<label> Password: </label>
<input type= “password” name= “password” /> <br/>
<input name=“b” type=“submit” value= “Submit” />
</form>
</body>
</html>
ex : using Post method‫استخدام‬
 In “welcome.php” we can retrieve the form elements
using $_POST variable:
<?php
# welcome.php
$user = $_POST ["username"];
$passwd = $_POST ["password"];
echo (“ Welcome $user “);
echo(“ <br /> your Password is : $passwd “);
?>
http://localhost/Form.html http://localhost/welcome.php
1 Login Form 2
Username: sustech Welcome sustech
Password: **** your Password is :1234
Submit
PHP Cookies: $_COOKIE
 A cookie is a small file that the server embeds on the
user's computer.‫عبارة عنملفص غير ي كونف يجهازا لمستخدم‬
 it is often used to identify a user.‫غا لبا ما ي ستخدم ل تع ريفا لمستخدم‬
 Creating cookie by setcookie(variable,value) cookie ‫انشاء‬
<?php setcookie ("user", "Alex Porter"); ?>

 Retrieving cookie value using ($_COOKIE variable): cookie‫ارجاع ق يمة‬

http://localhost/check.php
<?php
// check.php Print a cookie
Alex Porter
echo ( $_COOKIE["user"] );
// A way to view all cookies
Array ( [user] => Alex Porter )
print_r ($_COOKIE);
?>
PHP Session: $_SESSION
 PHP session variable is used to store information about user, or
change settings for a user session‫عبارة عنت خزينا لمعلوماتعنا لمستخدم‬

 Session variables are available to all pages‫هو متاح ف يك ل ا لصفحات‬

 session_start() function used to start session it


must appear in the first line of php file BEFORE
the <html> tag session‫عبارة عندا لة ل بداية‬

<?php session_start(); ?>


<html>
<body> </body>
</html>
Storing and retrieving Session variables:
 The correct way to store and retrieve session is to use the PHP
$_SESSION variable:‫ ا لطريقة ا لصحيحة ل تخزين‬$_SESSION ‫واسترجاع هو‬

ex: $_SESSION[‘view’] = 123;

 The isset() function checks if the variable has already


been set: ‫دا لة ا الختبار ا لمتغيرات‬
ex: if ( isset ($_SESSION[‘view’] ) )

$_SESSION[‘view’] ++;

 If you wish to delete some session data, you can use


the unset() function: unset( $_SESSION[‘views’] );

 session_destroy() function used to completely destroy


All the session data. session ‫ت دمير او ايقافا لدا لة‬
ex : using Get method and Session variable
Form.php

Login Form

Username:

Password:

Submit

Suppose we have a following session values:


$_SESSION['name'] = “sust";
$_SESSION['passwd']= "1234";
ex : using Get method and Session variable
<?php
session_start();
//store value on session
$_SESSION['name']= “sust";
$_SESSION['passwd']= "1234";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title> Using get & session variables - Login Form </title>
<body>
<h3> Login Form </h3>
<form action = “check.php" method= “get“ >
<label> Name : </label>
<input type= "text" name="username“/> <br/>
<label> Password: </label>
<input type= “password” name= “pass” /> <br/>
<input name=“b” type=“submit” value= “Submit” />
</form></body></html>
ex : using Get method and Session variable
 In “check.php” retrieve the form elements using $_GET
<?php
session_start();
$user = $_GET ["username"];
$pass = $_GET ["pass"];
if (($user == $_SESSION['name'] ) && ( $pass== $_SESSION[‘passwd'] ))
echo(“ Welcome $user “);
else
echo(“sorry, invalid user try again” );
include ('Form.php '); ?>
http://localhost/Form.php T http://localhost/check.php
1
Login Form Compare Welcome ali
Username: sdsust sust
1234 http://localhost/check.php
Password: ****
Submit F sorry, invalid user try again

You might also like