$GLOBALS
- all current global variables$_SERVER
- variables about the webserver or current script$_GET
- variables given by a GET request$_POST
- variables given by a POST request$_COOKIE
- HTTP cookies passed in the header$_FILES
- variables relating to file uploads over POST$_ENV
- Enviromental Variables$_REQUEST
- Like $_GET
, $_POST
and $_COOKIE
all concatenated together<?php
function print_as_table($array) {
print "<table><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>";
if($array) {
foreach(array_keys($array) as $key) {
print "<tr><td>$key</td><td>$array[$key]</td></tr>";
}
} else {
print '<tr><td colspan="2">None</td></tr>';
}
print "</tbody></table>";
}
?>
Available to view at https://www.csee.umbc.edu/~bwilk1/433/php_examples/web-variables.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Web Variables</title>
<style>
table { border-collapse: collapse; }
td, th { border: 1px solid #333333; }
</style>
</head>
<body>
<h3>$_SERVER</h3>
<?php print_as_table($_SERVER); ?>
<h3>$_COOKIE</h3>
<?php print_as_table($_COOKIE); ?>
<h3>$_GET</h3>
<?php print_as_table($_GET); ?>
<h3>$_POST</h3>
<?php print_as_table($_POST); ?>
<h3>$_ENV</h3>
<?php print_as_table($_ENV); ?>
<h3>$GLOBALS</h3>
<?php print_as_table($GLOBALS); ?>
</body>
</html>
<?php if($_SERVER['REQUEST_METHOD'] == 'GET') {?>
<form method="post">
#HTML for Form
</form>
<?php } else {
#Proecess form
}?>
Available at https://www.csee.umbc.edu/~bwilk1/433/php_examples/simple-form.php
<!DOCTYPE html>
<html lang="en">
<?php if($_SERVER['REQUEST_METHOD'] == 'GET') { ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div>
<label for="fahrenheit">Fahrenheit temperature:</label>
<input type="text" name="fahrenheit" id="fahrenheit" />
<input type="submit" value="Convert to Celcius" />
</div>
</form>
<?php } else {
$fahrenheit = $_POST['fahrenheit'];
$celcius = ($fahrenheit - 32) * 5 / 9;
print "<p>$fahrenheit Fahrenheit is $celcius Celcius</p>";
}
?>
</body>
</html>
[]
for PHP to know to do this<input type='checkbox' name='checks[]' value='hello'>Hello
<input type='checkbox' name='checks[]' value='hi'>Hi
<input type='checkbox' name='checks[]' value='howdy'>Howdy
$_GET['checks']
Available at https://www.csee.umbc.edu/~bwilk1/433/php_examples/checkboxes.php
<!DOCTYPE html>
<?php if($_SERVER['REQUEST_METHOD'] == 'GET') { ?>
<form action="<?php echo $_SERVER['_SELF'] ?>" method="post">
<p>Select Languages you know</p>
<p><input type="checkbox" name="languages[]" id="Perl" value="Perl" />
<label for="Perl">Perl</label></p>
<p><input type="checkbox" name="languages[]" id="JavaScript" value="JavaScript" />
<label for="JavaScript">JavaScript</label></p>
<p><input type="checkbox" name="languages[]" id="PHP" value="PHP" />
<label for="PHP">PHP</label></p>
<p><input type="checkbox" name="languages[]" id="Python" value="Python" />
<label for="Python">Python</label></p>
<p><input type="submit" value="Submit" /></p>
</form>
<?php } else {
print '<p>You know:</p>';
$languages = $_POST['languages'];
print_r($languages);
}
?>
</body>
</html>
<form enctype="multipart/form-data" action="some_url.php" method="POST">
<input type="file" name="my_file">
</form>
$_FILES
super global$_FILES['my_file']['name']
- The original name$_FILES['my_file']['type']
- The MIME type as reported by the uploader, not checked, don't trust!!$_FILES['userfile']['tmp_name']
- The name on the server where the file was uploadedtype
key, but you can trust finfo_file
function (PHP 5.3+)<!DOCTYPE html>
<html>
if($_SERVER['REQUEST_METHOD'] == 'GET') {
?>
<form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<div>
<label for="file">JPG File:</label>
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload" />
</div>
</form>
<?php
} else {
// check for type - browser can lie, also check in PHP
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if($_FILES['file']['type'] != 'image/jpeg' || finfo_file($finfo, $_FILES['file']['tmp_name']) != 'image/jpeg') {
print '<p>File does not appear to be a JPG. ' .
'<a href="' . $_SERVER['PHP_SELF'] . '">Try Again</a></p>';
// attempt to move to location
} else if (move_uploaded_file($_FILES['file']['tmp_name'], '/home/csee1/bwilk1/img.php')) {
print "<p>Success - file is " . FILE . "</p>";
// otherwise error
} else {
print "<p>An error occurred</p>";
}
}
?>
</body>
</html>
header
is used for this purpose<?php
header("Location: http://www.umbc.edu")
?>
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT")
?>
Content-Type
header tells the browser how to interperate the information it is receiving<?php
header('Content-Type: image/jpeg');
echo file_get_contents('myimage.jpg');
?>
<?php
header("Content-Type: image/png");
echo file_get_contents("maryland.png");
?>
<?php
header('Content-Type: application/pdf');
echo file_get_contents('https://about.umbc.edu/files/2017/09/2017-campus-map.pdf');
?>
<?php
header("Content-Type: video/webm");
echo file_get_contents("https://upload.wikimedia.org/wikipedia/commons/transcoded/2/26/Extraordinary_Adventures_of_Saturnino_Farandola_%281913%29.webm/Extraordinary_Adventures_of_Saturnino_Farandola_%281913%29.webm.360p.webm");
?>
<?php
echo file_get_contents('maryland.png');
?>
class className
{
public/private $variableName
public/private function functionName(){}
}
class my_first_class{
public $a_var;
public function get_var(){
return $this->a_var;
}
}
$obj = new my_first_class();
$obj->a_var = 10;
echo $obj->get_var();
__construct
(The object constructor)__destruct
(What is called when garbage collection is performed)__toString
(Used in print or echo)class Person{
private $name;
private $age;
function __construct($name = "Jane Doe", $age = 30){
$this->name = $name;
$this->age = $age;
}
function __toString(){
return $this->name . " is ". $this->age;
}
}
$p1 = new Person();
echo $p1;
$p2 = new Person("Timmy");
echo $p2;
$p3 = new Person("Harry", 45);
echo $p3;
extends
keywordparent
keyword::
operator to access a method staticllyclass Employee extends Person{
private $workplace;
function __construct($workplace = "UMBC"){
parent::_construct();
$this->workplace = $workplace;
}
function __construct($name="Joe Student", $age=19, $workplace = "UMBC"){
parent::_construct($name,$age);
$this->workplace = $workplace;
}
function __toString(){
return parent::__toString . " and works at " . $this->workplace;
}
}
interface name{
public function name(); #Note there is no definition
}
static
after the access modifier::
self::
const
keywordclass my_static{
private static $counter = 0;
const UPDATE_BY = 1;
function increment(){
self::$counter += self::UPDATE_BY;
return self::$counter;
}
}
$ms1 = new my_static();
echo $ms1->increment();
$ms2 = new my_static();
echo $ms2->increment();
$_SESSION
with the appropriate valuessession_start()
function in your PHP codeAvailable at https://www.csee.umbc.edu/~bwilk1/433/php_examples/session-store.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<?php if($_SERVER['REQUEST_METHOD'] == 'GET') { ?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
<input type="submit" value="Submit" />
</div>
</form>
<?php } else {
$_SESSION['name'] = $_POST['name'];
$name = $_POST['name'];
print "<p>Hello, $name</p>";
}
?>
</body>
</html>
Available at https://www.csee.umbc.edu/~bwilk1/433/php_examples/session-recall.php
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Recall Things from Session</title>
</head>
<body>
<p>Hello, <?php echo $_SESSION['name'] ?></p>
</body>
</html>
json_encode()
- Turns PHP object into JSONjson_decode()
- Smartly produces an array/object/etc.$an_array = array(1,2,3,5,6);
echo json_encode($an_array);
$an_object->date = "November 21, 2017";
$an_object->time = "12:??";
echo json_encode($an_object);
$an_assoc_array = array('a' => 20, 'b' => 30);
echo json_encode($an_assoc_array);
$p1 = new Person();
echo json_encode($p1);
echo print_r(json_decode("[1,2,3,4,5]"),true);
echo print_r(json_decode('{"name":"John Watson","dob":"November 10th 1860","birthplace":"London"}')
,true);