You are on page 1of 40

Array in PHP

GET, POST, and REQUEST


PHP Include File
Cookie and Session

What is an Variable?
A variable is a storage area holding a number or text.
The problem is, a variable will hold only one value.

What is an Array?
An array is a special variable, which can store multiple
values in one single variable.

Numeric array - An array with a numeric index


Associative array - An array where each ID key is
associated with a value
Multidimensional array - An array containing one or
more 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):
$nama
Akhmad
Agus");
2. In the following example we assign the index manually:
$nama[0]="Akhmad ";
$nama[1]="Denny";
$nama[2]="Teddy";
$nama
Agus";

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.
1. In this example we use an array to assign ages to the different persons:
$umur= array Munanto"=>32, Sevtian"=>30, Aldi"=>34);
2. This example is the same as example 1, but shows a different way of creating the
array:
$ umur Munanto'] = "32";
$ umur Sevtian'] = "30";
$ umur Aldi'] = "34";

$datakuliah
$datakuliah
$datakuliah
$datakuliah
datakuliah
datakuliah

Anto

Anto

sort
rsort
asort -- elemen
arsort
ksort -- key
krsort
natsort -- man
array_reverse
array_pop
array_push

array_shift
array_unshift
array_rand
array_unique
in_array
shuffle
range
explode
implode

The predefined $_GET variable is used to collect values in a form


with method="
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.
Note:
This method should not be used when sending passwords or other
sensitive information!
The get method is not suitable for very large variable values. It should not
be used with values > 2000 characters.

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

Address Bar:
welcome.php?fname=Peter&age=37
Welcome.php:
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

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.

Example:
Form:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Address Bar:
welcome.php
Welcome.php:
Welcome <?php echo $_POST["fname"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!

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.

<form method="POST"
name="myform"
action="form.php">
Nama = <input
type="text" name="txtnama"
size="20"><br>
<input type="button"
value="Simpan" name="simpan"
onClick="huruf(this.form)"><inp
ut type="reset" value="Reset"
name="B2">
</form>
<script>
function check(form) {
if(form.txtnama.value=="") {
alert("Nama belum diisi!");
form.txtnama.focus();
return false;
} else
return true;
}

function huruf(form) {
if (check(form)) {
if(!isNaN(form.txtnama.value)) {
alert("Nama harus
huruf!");
form.txtnama.focus();
return false;
} else
form.submit();
return true;
}
}
document.myform.txtnama.focus();
</script>

<body>
<?php
echo "Nama Anda ".$_POST['txtnama'];
?>
</body>

Legal access

Direct access

<body>
<?php
if(isset($_POST['txtnama'])) {
echo "Nama Anda : ".$_POST['txtnama'];
}
?>
</body>

Legal access

Direct access

<body>
<?php
if(isset($_POST['txtnama'])) {
echo "Nama Anda : ".$_POST['txtnama'];
} else {
echo "Illegal Access is not allowed!";
}
?>
</body>

Legal access

Direct access

<body>
<?php
if(isset($_POST['txtnama'])) {
echo "Nama Anda : ".$_POST['txtnama'];
} elseif(isset($_GET['txtnama'])) {
echo $_GET['txtnama'].", this method is not allowed!";
} else {
echo "Illegal Access is not allowed!";}
?>
</body>

Legal access with post mehod

Denied access with get

You can insert the content of one PHP file into


another PHP file before the server executes it, with the
include() or require() function.
The two functions are identical in every way, except how
they handle errors:
include() generates a warning, but the script will continue
execution
require() generates a fatal error, and the script will stop

These two functions are used to create functions, headers,


footers, or elements that will be reused on multiple pages.

<html>
<body>
<?php include("header3.php"); ?>
<h1>Selamat Datang!</h1>
<p>hehehe....</p>
</body>
</html>

<html>
<body>
<?php require("header3.php"); ?>
<h1>Selamat Datang!</h1>
<p>hehehe....</p>
</body>
</html>

The term of function include_once() same with include.


The different is function include_once only include one time.

Example:
<?php

?>

<?php

?>

var.php
$var = "nilai var ke-1<br/>";

include_once.php

What the result of this


code?

include("var.php");
echo $var;
$var = "nilai var ke-2<br/>";
echo $var;
include_once("var.php");
echo $var;

The term of function require_once() same with require.


The different is function require_once only require one time.

Example:
<?php

?>
<?php

?>

var.php

$var = "nilai var ke-1<br/>";


require_once.php

What the result of this


code?

require("var.php");
echo $var;
$var = "nilai var ke-2<br/>";
echo $var;
require_once("var.php");
echo $var;

What is a Cookie?
A cookie is a small file that the server embeds on the user's
computer.

Example:
<?php
setcookie(nama, Alex Porter);
?>
<html>
<body>
</body>
</html>

Set the Cookie


setcookie(cookie_name,cookie_value);
setcookie nama
Warning : place before <html>

Get individual cookie


$

nama
nama

View all cookie


print_r($_COOKIE);

Change the cookie value


setcookie(cookie_name,new_value);
setcookie nama

Expiration
setcookie(cookie_name, cookie_value, expired_date);
setcookie nama
()+3600);

The cookie will be expired on 3600 seconds (1 hour).

Remove the Cookie


setcookie(cookie_name);
setcookie nama
()); //recomended
setcookie nama
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);

A PHP session variable is used to store information about, or change


settings for a user session. Session variables hold information
about one single user, and are available to all pages in one
application.
With session the computer knows who you are.
But on the internet there is one problem: the web server does not
know who you are and what you do because the HTTP address doesn't
maintain state.
In sort: session is temporary variable that is stored on a computer
server.

Start the Session


session_start(); //place before <html>

Ended specific Session


unset($_SESSION session_name']);

Ended all Session


session_destroy(); //place before <html>

Set the Session

$_SESSION[session_name]=session_value;
Example

$_
$

nama
nama

Get the Session


$
$

nama
nama

Change the session value


$_
$

nama
nama

Make simple form login for admin with session


Give information if success or failed login with cookie

You might also like