PHP-HTTP and Sessions-Maintaining State

HTTP has no mechanism to maintain state; thus HTTP is a context-free or stateless protocol. Individual requests aren't related to each other. The Web server and thus PHP can't easily distinguish between single users and doesn't know about user sessions. Therefore, we need to find our own way to identify a user and associate session data that is, all the data you want to store for a user with that user. We use the term session for an instance of a user visiting a site where one or more pages are viewed. For example, a typical online shopping session might include putting an item into the shopping cart, going to the checkout page, entering address and credit card data, submitting the order, and closing the browser window.
 
At first, the typical PHP programmer tries to ignore the problem and find a workaround for it. The obvious workaround is to store all data on the client instead of on the server. This leads to forms with a lot of hidden fields or very long URLs. It becomes impractical with more than two files and more than one variable to save. An only slightly more intelligent method is to use cookies to store all information on the client side.

  • You lose control over the data—as long as the user doesn't return to your site, you can't access the data. And worse, that data may be manipulated when you get it back. Ninety percent of all Web site defacing and breakings come from applications accepting tampered data from the client side and trusting that data. Do not keep data on the client. Do not trust data from the client.
  • If you use GET/POST, the storage isn't persistent across sessions.
  • If you rely exclusively on cookies, you have a problem because some users won't accept cookies—they simply disable cookies in their browsers.
  • The data is hard to maintain because you need to save all data on every page. Each variable needs to be URL-encoded, added to a form as a hidden field or added to the URL, or saved as a cookie. This is difficult for a single variable such as the session ID, let alone dozens of variables!
Thus, the data needs to be stored on the server. Where exactly you store it isn't all that important; it can be in a relational database management system RDBMS, plaintext file, dBASE file, etc. Because a Web application generally already uses a relational database such as MySQL, this should be the preferred storage medium.

The typical PHP programmer tries to ignore the problem and find a workaround for it. The obvious workaround is to store all data on the client instead of on the server. This leads to forms with a lot of hidden fields or very long URLs. It becomes impractical with more than two files and more than one variable to save. An only slightly more intelligent method is to use cookies to store all information on the client side.

PHP has a built-in uniqid() function, but because it's based on the system time, it's not secure enough to be used for a session ID. However, you can combine it with a hash function and rand() to
 construct a truly 

srand((double)microtime()*1000000); // Seed the random number generator
$session_id = md5(uniqid(rand())); // Construct the session ID
 
By the way, md5(uniqid())—the same construct from above without a 
rand() call—would not be sufficiently random; because uniqid() 
is based on the system time, it can be guessed if the hacker learns the local 
system time of the server.  


Related Posts:
  • What is LAMP? LAMP means combination of Linux, Apache, MySQL and PHP. … Read More
  • Storing Complex Data Types You can use sessions to store complex data types such as objects and arrays simply by treating them as standard variables, as this code shows: $myarr["0"] = "Sunday"; $myarr["1"] = "Monday"; $myarr["2"] = "Tue… Read More
  • php file_get_contents This function is the preferred way to read the contents of a file into a string. The function itself does nothing but puts the source of the web page you supply it into a string available for us… Read More
  • Check if image file ?? allow_url_fopen is activated in your PHP config     $filename = "http://".$_SERVER['SERVER_NAME']." /media/img/".$row['CatNaam'].".jpg"; echo" <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">"; … Read More
  • thumbnail creation PHP script   To create a thumbnail, first check file entenson, and then read in the file using the imagecreatefromjpeg() or imagecreatefrompng() or imagecreatefromgif() function and can calculate the new thumbnail size. imag… Read More
  • mod_rewrite? Rewrites the requested URL on-the-fly based on configuration directives and rules. You are using system paths. Apache mod_rewrite only works with URLs,   RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} … Read More
  • Pagination in PHP and MySQL <?php function pagination($per_page = 10, $page = 1, $url = '', $total){ $adjacents = "2"; $page = ($page == 0 ? 1 : $page); $start = ($page - 1) * $per_page; $prev = $page - 1; $next = $page + 1; $lastpage = ceil… Read More
  • Use $_POST to get input values It will execute the whole file as PHP. The first time you open it,  $_POST['submit']  won't be set because the form has not been sent.    <?php if (isset($_POST['submit'])) { $example = $_POST['… Read More
  • Cannot modify header information Check that <?php is at the very start of the functions.php file (before any whitespace) and remove ?> at the end of that file. header or setcookie or anything else that sends HTTP headers has  to be done before&n… Read More
  • How get the value of current session id? Session_id() returns the session id for the current session. … Read More
  • php exec The exec() function is one of several functions you can use to pass commands tothe shell. The exec() function requires a string representing the path to the commandyou want to run, and optionally accepts an array variable th… Read More
  • php interview questions PHP Interview Questions and Answers Click  this  link   … Read More
  • PHP Write and Read from File $fp = @fopen ("text1.txt", "r"); $fh = @fopen("text2.txt", 'a+'); if ($fp) { //for each line in file while(!feof($fp)) { //push lines into array $thisline = fgets($fp); $thisline1 = trim($thisline); … Read More
  • What are the different tables present in mysql? Total 5 types of tables we can create 1. MyISAM 2. Heap 3. Merge 4. InnoDB 5. ISAM 6. BDB MyISAM is the default storage engine … Read More
  • Encoding php file str_rot13() base64_decode()   Try ionCube PHP Encoder,  link - http://www.ioncube.com/sa_encoder.php        There are only two worthwhile players in the encoding market, Zend Guard ($600) and i… Read More