Student PHP1
Student PHP1
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
second.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
echo "All session variables are now removed, and the session is
destroyed."
?>
</body>
</html>
Include Files
'footer.php'
<?php
echo "<p>Copyright © </p>";
?>
index.php
<!DOCTYPE html>
<html>
<body>
The require statement is also used to include a file into the PHP
code.
However, there is one big difference between include and
require; when a file is included with the include statement and
PHP cannot find it, the script will continue to execute:
If we do the same example using the require statement, the echo
statement will not be executed because the script execution dies
after the require statement returned a fatal error:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
File Handling
The PHP code to read the file and write it to the output buffer is
as follows (the readfile() function returns the number of bytes
read on success)
<!DOCTYPE html>
<html>
<body>
<?php
echo readfile("webdictionary.txt");
?>
</body>
</html>
File Open/Read/Close
If you are having errors when trying to get this code to run,
check that you have granted your PHP file access to write
information to the hard drive.
PHP Write to File - fwrite()
A cookie is often used to identify a user. A cookie is a small file that the
server embeds on the user's computer. Each time the same computer
requests a page with a browser, it will send the cookie too. With PHP,
you can both create and retrieve cookie values.
Syntax
• setcookie(name, value, expire, path, domain, secure, httponly);
• We then retrieve the value of the cookie "user" (using the global
variable $_COOKIE). We also use the isset() function to find
out if the cookie is set:
Example
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Note: The setcookie() function must appear BEFORE the <html> tag.
Example
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Delete a Cookie
Example
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>