All posts by Pramod T P

Difference between mysql_connect() and mysql_pconnect() PHP

Difference between mysql_connect() and mysql_pconnect() PHP

mysql_pconnect() acts very much like mysql_connect() with two major differences.

When connecting using mysql_pconnect() , the function would first try to find a (persistent) link that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

When connecting using mysql_connect(), the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use

Note :mysql_close() will not close links established by mysql_pconnect().

CAPTCHA

A CAPTCHA is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a person. The process usually involves a computer asking a user to complete a simple test which the computer is able to grade. These tests are designed to be easy for a computer to generate, but difficult for a computer to solve, so that if a correct solution is received, it can be presumed to have been entered by a human. A common type of CAPTCHA requires the user to type letters or digits from a distorted image that appears on the screen, and such tests are commonly used to prevent unwanted internet bots from accessing websites.

The term “CAPTCHA” was coined in 2000 by Luis von Ahn, Manuel Blum, Nicholas J. Hopper, and John Langford (all of Carnegie Mellon University). It is an acronym based on the word “capture” and standing for “Completely Automated Public Turing test to tell Computers and Humans Apart”. Carnegie Mellon University attempted to trademark the term, but the trademark application was abandoned on 21 April 2008.

form validation

<?php
extract($_POST);
if(isset($_POST[‘send’])){
if(preg_replace(“/^s*$/”,””,$firstname)==”” )
$errorList[]=”Select the department name is required.”;

if(preg_replace(“/^s*$/”,””,$email)==”” )
$errorList[]=”Email is required.”;
elseif(!eregi(“^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$”, $email))
$errorList[] = “Email address is invalid.”;

if(preg_replace(“/^s*$/”,””,$message)==”” )
$errorList[]=”Message is required.”;

?>
<form action=”#” method=”post”>
<table>
<?php if(count($errorList)>0){ ?>
<tr>
<td>&nbsp;</td><td><div style=”margin:5px; color:#FF0000;”><?php foreach($errorList as $value) echo $value.” ,”; ?></div></td></tr>
<?php } ?>
<?php if($success==5){ ?>
<tr>
<td>&nbsp;</td> <td><div style=”margin:10px; color: #006633; width:90%; clear:none; float:left; font-weight:bold”>Your message has been sent to site admin .</div></td>
</tr>
<?php } ?>

<tr>
<td>Please enter your <strong>name</strong>:</td> <td><input name=”firstname” type=”text” value=”<?php echo $firstname ?>” /></td>
</tr>

<tr>
<td>Please enter your <strong>email address </strong>:</td> <td><input name=”email” type=”text” value=”<?php echo $email ?>” /></td>
</tr>

<tr>
<td>Please enter message:</td> <td><textarea name=”message” style=”height:50px;”><?php echo nl2br($message); ?></textarea></td>
</tr>

<tr>
<td>&nbsp;</td> <td><input type=”submit” name=”send” value=” Send “></td>
</tr>

</table>

cookie

Cookies can be used to store user information for future use . Cookies allow us to store data in users’ machine it self . Cookie is a small file that the server generate in user machine and using that file server can identify the user .When ever the browser send http request to the server , it send the cookies as well . Cookies can be created using PHP functions .

Cookies can be created using the function ‘ setcookie(name,Value ,exp,path,domain)’ and its arguments are given below

Name :Name of the cookie that store the value and it is using to retrieve the stored data

Value : Its the value to be stored in the cookie ()generally we store login details like username , password)

exp : This is the the time that cookie lasts . if its not set ,the cookie will get destroyed when the browser closed .

path : This is path where the cookie to be stored

Domain: Domain where the cookie to be generated

The following can be used to send a cookie from within a PHP application.

  • header()
  • setcookie()
  • setrawcookie()

Example
======

<?php
 setcookie(“user_name”, “phpcodez”, time()+3600);
 ?>

You can retrieve the cookie values as follows

<?php
 echo $_COOKIE["user_name"];
 ?>

You can delete the cookie by setting expiration date in the past

Example:

<?php
 setcookie(“user”, “”, time()-3600);
 ?>