URLs/Links |
GET |
Forms |
POST |
Cookies |
COOKIE |
$_GET[‘’] & urlencode(), urldecode
Information can be sent in the link request and retrieved by the receiving page
<a href="secondpage.php?id=2">Second Page</a><br />
<a href="secondpage.php?name=Kevin Lastname&id=42">Second Page - name=Kevin</a><br />
<a href="secondpage.php?name=<?php echo urlencode("Kevin&") ?>&id=42">Second Page - Kevin& using urlencode()</a>
<br />---This link uses urlencode() and receiver will need urldecode
<br />---<?php echo urlencode("Kevin&") ?>
print_r($_GET);
$id = $_GET['id'];
if (!$_GET['name']){
$name = "none";
} else {
$name = $_GET['name'];
$name2 = urldecode($_GET['name']);
}
First Page Array ( )
Name RAW:none ID:
Name RAW:none
Name: urldecode():
Name: urlencode():
Name: htmlspecialchars():none
Encoding
Code
<?php
$url_page = 'php/create/page/url.php';
$param1 = 'this is a string';
$param2 = '"bad"/<>character$';
$linktext = "<Click> & you'll see";
?>
<?php
// this gives you a clean link to use
$url = "http://e6510/";
$url .= rawurlencode($url_page);
$url .= "?param1=" . urlencode($param1);
$url .= "¶m1=" . urlencode($param2);
// htmlspecialchars esacepes any html that
// might do bad things to your html page
?>
<a href="<?php echo $url; ?>">
<?php echo htmlspecialchars($linktext);?>
</a>
Ouput
Forms
<form action="process.php" method="post">
Username: <input type="text" name="username" value="" />
Password: <input type="password" name="password" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo "{$username}: {$password}";
?>
MyName: 1234
Cookies
-
setcookie() creates a cookie for a set time
<?php
setcookie('test', 45, time()+(60*60*24*7));
?>
-
Access the cookie with the $_COOKIE[‘’] variable
<?php
$var1 = $_COOKIE['test'];
echo $var1;
?>
45
Removing a cookie
-
setcookie('test', 0, time()-(60*60*24*7));
Check for COOKIE presence with isset()
<?php
// init $var1 in case COOKIE is empty/missing
$var1 = 0;
if (isset($_COOKIE['test'])){
$var1 = $_COOKIE['test'];
}
echo $var1;
?>
Sessions
-
Sessions store data on the server under a session ID.
-
The client computer stores the Session ID in a cookie called PHPSESSID.
<?php
// must occur before any HTML
session_start();
?>
-
Session ID is stored in a cookie on client computer
3r0lf8ggvepvu6t3hdhrfqs892
<html>
<body>
<?php
$_SESSION['name'] = "kevin";
?>
<?php
$name = $_SESSION['name'];
echo $name;
?>
</body>
</html>
-
Session files on the server can take up space over time either with multiuser sites or sessions that store large amounts of data
Other SUPER GLOBALS
$_SERVER
$_HOST
Headers and Page Redirection
-
Server makes a request to the php page, it will send headers to browser before HTML data
-
Headers precede HTML data
header(header information)
header(“Content-type: application/vnd.ms-excel; name=’excel’”);
header(“Content-disposition: attachment; filename=myfile.xls”);
<?php
header("HTTP/1.0 404 Not Found");
exit;
// headers must occur before any HTML
?>
Redirect
<?php
// headers must occur before any HTML
// this is how you redirect a page
// 302 Redirect
header("Location: basic.html");
?>
<?php
// this is how you return a 404 error
//header("HTTP/1.0 404 Not Found");
exit;
?>
Output buffering
-
Instead of running into problems with php before HTML, Server will store up all html code until all data parsed and php code handled.
-
If output buffering is enabled, the header() command will still work if within the HTML body tag.
-
PHP.ini controls this feature.
; Output buffering is a mechanism for controlling how much output data
; (excluding headers and cookies) PHP should keep internally before pushing that
; data to the client. If your application's output exceeds this setting, PHP
; will send that data in chunks of roughly the size you specify.
; Turning on this setting and managing its maximum buffer size can yield some
; interesting side-effects depending on your application and web server.
; You may be able to send headers and cookies after you've already sent output
; through print or echo. You also may see performance benefits if your server is
; emitting less packets due to buffered output versus PHP streaming the output
; as it gets it. On production servers, 4096 bytes is a good setting for performance
; reasons.
; Note: Output buffering can also be controlled via Output Buffering Control
; functions.
; Possible Values:
; On = Enabled and buffer is unlimited. (Use with caution)
; Off = Disabled
; Integer = Enables the buffer and sets its maximum size in bytes.
; Note: This directive is hardcoded to Off for the CLI SAPI
; Default Value: Off
; Development Value: 4096
; Production Value: 4096
; http://php.net/output-buffering
output_buffering = 4096
Usage
ob_start();
<html>
<head>
<title>My Page</title>
</head>
<body>
Page Content
</body>
<html>
ob_end_flush();
Include and Require
-
include()
-
include a php file
-
no error if not found
-
-
include_once()
-
include a php file
-
but only once
-
Good for php function files.
-
-
require()
-
include a php file
-
throw error if not found
-
-
require_once()
-
include a php file
-
but only once
-
throw error if not found
-
Good for php function files.
-
included_func.php:
functions
includes.php
<?php
include("included_func.php");
?>
Output:
functions
Example2: Calling included function
included_func.php:
<?php
function hello($name){
echo "Hello {$name}";
}
?>
includes.php
<?php
include("included_func.php");
?>
<?php
hello("Eveyone");
?>
Output:
Hello Eveyone
- Log in to post comments