Thursday, May 29, 2014

generate QR code by google api php

In this post we explain you, how you can generate QR code with metadata by google api. You can generat qr code for any link, email, telephone number or text data.

For generate code, you can use following php script:


<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


function google_qr_code($data, $type = "TXT", $size ='150', $ec='L', $margin='0')  {     
    $types = array("URL" => "http://", "TEL" => "TEL:", "TXT"=>"", "EMAIL" => "MAILTO:");
    if(!in_array($type,array("URL", "TEL", "TXT", "EMAIL")))
    {
        $type = "TXT";
    }
    if (!preg_match('/^'.$types[$type].'/', $data))
    {
        $data = str_replace("\\", "", $types[$type]).$data;
    }
    $ch = curl_init();
    $data = urlencode($data);
    curl_setopt($ch, CURLOPT_URL, 'http://chart.apis.google.com/chart');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'chs='.$size.'x'.$size.'&cht=qr&chld='.$ec.'|'.$margin.'&chl='.$data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $response = curl_exec($ch);

    curl_close($ch);
    return $response;
}

header("Content-type: image/png");

echo google_qr_code("4evertutorials","TXT");
//echo google_qr_code("http://4evertutorials.blogspot.com","URL");
//echo google_qr_code("4evertutorial@gmail.com","EMAIL");
//echo google_qr_code("1234567890","TEL");




?>

Tuesday, May 27, 2014

PHP Expire Session after 30 minutes

In following php script, we explain you that how you can expire session after 30 minutes. Execution a session timeout on your own is that the best answer. Use a straightforward time stamp that denotes the time of the last activity and update it with each request.
CODE USAGE


<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) 
{
    /* last request was more than 30 minutes ago */

    // unset $_SESSION variable for the run-time
    session_unset();     

    // destroy session data in storage
    session_destroy();   

}

// update last activity time stamp
$_SESSION['LAST_ACTIVITY'] = time(); 


// You can also use an additional time stamp to regenerate the session ID periodically to avoid attacks on sessions like session fixation:

if (!isset($_SESSION['CREATED'])) 
{
    $_SESSION['CREATED'] = time();
}
else if (time() - $_SESSION['CREATED'] > 1800)
{
    /* session started more than 30 minutes ago */

    // change session ID for the current session an invalidate old session ID
    session_regenerate_id(true);

    // update creation time
    $_SESSION['CREATED'] = time();  

}

?>
 

© 2014 4everTutorials. All rights resevered.

Back To Top