Php
- 1. PHP
FILE UPLOAD:
To upload files from your browser to your hosting, using PHP,
the first thing we need to do is create an HTML form that
allows people to choose the file they want to upload.
<form enctype="multipart/form-data"
action="upload.php" method="POST">
Please choose a file:
<input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
This form sends data to the file "upload.php", which is
what we will be creating next to actually upload the file.
The actual file upload is very simple:
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']
['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']
['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']
['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}?>
- 2. This very small piece of code will upload files sent to it by
your HTML form.
1.The first line $target = "upload/"; is where we assign the
folder that files will be uploaded to. As you can see in the
second line, this folder is relative to the upload.php file
2.We are not using $ok=1; at the moment but we will later in
the tutorial.
3.We then move the uploaded file to where it belongs using
move_uploaded_file (). This places it in the directory we
specified at the beginning of our script. If this fails the user
is given an error message, otherwise they are told that the
file has been uploaded.
SESSION:
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.
- 3. Starting a PHP Session:
Before you can store user information in your PHP session,
you must first start up the session.
<?php session_start(); ?>
<html>
<body>
</body>
</html>
The code above will register the user's session with the
server, allow you to start saving user information, and
assign a UID for that user's session.
Storing a Session Variable:
The correct way to store and retrieve session variables is to
use the PHP $_SESSION variable:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
</body>
</html>
- 4. Output:
Pageviews=1
Destroying a Session:
If you wish to delete some session data, you can use the
unset() or the session_destroy() function.
The unset() function is used to free the specified session
variable:
<?php
unset($_SESSION['views']);
?>
You can also completely destroy the session by calling the
session_destroy() function:
<?php
session_destroy();
?>
COOKIES:
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.
How to Create a Cookie?
The setcookie() function is used to set a cookie.
setcookie(name, value, expire, path, domain);
- 5. How to Retrieve a Cookie Value?
The PHP $_COOKIE variable is used to retrieve a cookie
value. Example
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
How to Delete a Cookie?
When deleting a cookie you should assure that the expiration
date is in the past. Example
<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>
TYPES OF ERRORS
1.Notices: These are trivial, non-critical errors that PHP
encounters while executing a script - for example,
accessing a variable that has not yet been defined. By
default, such errors are not displayed to the user at all -
although you can change this default behavior.
- 6. 2.Warnings: These are more serious errors - for example,
attempting to include() a file which does not exist. By
default, these errors are displayed to the user, but they do
not result in script termination.
3.Fatal errors: These are critical errors - for example,
instantiating an object of a non-existent class, or calling a
non-existent function. These errors cause the immediate
termination of the script, and PHP's default behavior is to
display them to the user when they take place.
PHP error_log() Function
The
error_log() function sends an error to the server
error log, a file or a remote destination.
This funtion returns TRUE on success, or FALSE on
failure.
Syntax:
error_log(error,type,destination,headers)
PHP parse_ini_file() Function
The parse_ini_file() function parses a configuration (ini)
file and returns the settings in it in an array.
Syntax:
parse_ini_file(file,process_sections)