1- PHP & FTP
Creating a PHP class for handling some FTP tasks can be very usefull and will be used in narly all your development projects. We will shed some light on how you might build this FTP class to automate uploading of images or to backup sites to remote web server or file server using this FTPclass
FTP, or File Transfer Protocol, is, as defined by Wikipedia: “A standard network protocol used to copy a file from one host to another over a TCP/IP-based network, such as the Internet.”
1- Create two PHP files named ftp.php ( we will write our class in this file)
<code>
<?php
Class FTP {
private $server;
private $ftp_username;
private ftp_password;
public function __construct($server, $ftp_username, $ftp_password) {
$this->server = $server;
$this->ftp_username = $ftp_username;
$this->ftp_password = $ftp_password;
}
public function connect() {
$ftp_connect = ftp_connect($this->server);
$ftp_login = ftp_login($ftp_connect, $this->ftp_username, $this->ftp_password);
}
}
</code>
The strength of Object-Oriented Programming (OOP) is to give complex code an easy to use interface. By creating a class – think of a class as a pattern – you can encapsulate the data, which is simply jargon for a term that refers to hiding the data. We can then reuse this class over and over without the need to rewrite any of the code. Instead, you only need to call the appropriate methods (the term “method” is the same as function).
2- PHP Sessions
A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.
The location of the temporary file is determined by a setting in the file called session.save_path. Before using any session variable make sure you have setup this path.
PHP first creates a unique identifier for that particular session which is a random string of 32 hexadecimal numbers such as 3c7foj34c3jj973hjkop2fc937e3443.
A cookie called PHPSESSID is automatically sent to the user’s computer to store unique session identification string.
A file is automatically created on the server in the designated temporary directory and bears the name of the unique identifier prefixed by sess_ ie sess_3c7foj34c3jj973hjkop2fc937e3443.
When a PHP script wants to retrieve the value from a session variable, PHP automatically gets the unique session identifier string from the PHPSESSID cookie and then looks in its temporary directory for the file bearing that name and a validation can be done by comparing both values.
A session ends when the user loses the browser or after leaving the site, the server will terminate the session after a predetermined period of time, commonly 30 minutes duration.
Starting a PHP Session
A PHP session is easily started by making a call to the session_start() function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page.
Session variables are stored in associative array called $_SESSION. These variables can be accessed during lifetime of a session.
To start a session you can use
<code>
<?php
sessions_start();
?>
</code>
at the beginneg of your php file, and to put data in the $_SESSION variable you can use this syntax:
<code>
<?php
sessions_start();
$_SESSION[‘user_ID’] = $this->getUserID();
?>
</code>
this code will create an entry in the $_SESSION variable with key “user_ID”, and can be accessed any where in your php script.
to destroy all sessions you can use this code
<code>
<?php session_destroy(); ?>
</code>
Turning on Auto Session