Wednesday 12 December 2012

PHP FAQ

How to secure get parameters in php ?

Try using

sending end

$a="some crap";
$key = 'pass';// can use your own key here
$string = " ".$a." ";
$encrypteda = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));



receiving end

$key = 'pass';
$rand=($_GET['parameter']);
$rand = str_replace(" ", "+", $rand);
$rand = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($rand), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
$rand = str_replace(" ", "", $rand);



--------------------------------------------------------------------------------------------------------------------


 How to download multiple files in zipped form ?
Want to download files gmail style ? , well you can do it by using simple php script
we shall be using the following approach

open a ziparchive

add files to the ziparchive (you can add multiple files here )

close it after adding

Now just pass a header information containing the name of zip archive

finally read the archive file


That would be -
a reference to archive (inspired by java i guess!)

$ziparch= new ZipArchive()

open an archive
$ziparch->open($somename);

Then add files by

A loop
{
$ziparch->addFile($filename);
 }

 Close once done
$ziparch->close();

followed by provision of header information indicating the ziparchive name

header("Content-type: application/zip");
    header("Content-Disposition: attachment; filename=$somename");
    header("Pragma: no-cache");
    header("Expires: 0");


finally

readfile("$somename");



--------------------------------------------------------------------------------------------------------------------


How to pass data between php pages ?
If you intend to pass data and yet find the popular post and get method to be incompatible use session .

all you got to do is start session at the beginning of the script

session_start()


Add data to session variables
(say in page 1)

$_SESSION['identifier']=$data;


Retrieve data by
(say in page 2)

$get_data=$_SESSION['identifier'];




--------------------------------------------------------------------------------------------------------------------



How to periodically execute php scripts ?
Without any user pinging the server, you can use cron service to schedule execution of scripts.Check out crontab in your favorite search engine.But there is a catch ,you have to incorporate this service onto the server and in some of the cases it is non trivial.
another method is to use the php's time() which will give the unix time stamp. well You can use just this.

Consider a time() instant   1358185093   ,get the ,say last 5 numbers   or  85093
now in your index.php or in your php script use this

if($last_5_digit == 50000)
{
 execute a periodic script.
}

NOTE : this works good for servers which get heavy traffic,since the users are constantly pinging the server (even when time()(last 5) == 5000)

The best ,easier method which i generally use is to add a time field in a table and observe the current time .Then delete all rows whose time field and the current time difference is beyond a threshold.



------------------------------------------------------------------------------------------------------------------------------

How to ping urls when your server is behind a proxy ?


use

$acontext = array(
    'http' => array(
        'proxy' => 'tcp://proxy_server:port',          // example tcp://10.10.10.10//3128
        'request_fulluri' => true,
    ),
);

$cxContext = stream_context_create($acontext);
 $geturlcontents = file_get_contents("$url", False, $cxContext);





Incidentally for getting web resource in python

use

html=urllib2.urlopen('http://python.org/').read()





but ensure in case of proxy server,to set your environment variable http_proxy

use            $ export http_proxy=http://proxy_server:port


$ echo $http_proxy            should echo something like http://proxy_server:port/