Get Browser Details Using PHP

This PHP function can be used to get the details of the browser.


function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";

//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}

// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}
else
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
//echo "B: ".$bname."<br>";

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}

// see how many we have
$i = count($matches['browser']);
//echo "i: ".$i."<br>";
if($i==0)
{
$version = 11;
}
elseif ($i != 1 && $i!=0) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}
//echo "V: ".$version;exit;

// check if we have a number
if ($version==null || $version=="") {$version="?";}

return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}

Tested In all the major browsers.

Advertisements

Go back to the previous page in PHP

Although there are many methods to do this, I prefer the following code to go back to the previous page in PHP.

11


session_start();
$url = basename($_SERVER['PHP_SELF']);
$query = $_SERVER['QUERY_STRING'];
if($query){
$url .= "?".$query;
}
$_SESSION['current_page'] = $url;

In the page you want to come back to, add the above code. In the variable $url, even the query string attached to the added. This helps in case you want to go back to page like userprofile.php?userid=1234567 .Store this in a session variable.

In the second page, get the value from the session variable.


session_start();
$previous_page = $_SESSION['current_page'];

To go back to the previous page using a button:


<a href="<?php echo $previous_page;?>"><button>BACK</button></a>

Or

By Using header:


header("location:$previous_page");

Hope this helps!

Re-sampling Image In PHP

When a user uploads a high quality image, you might have to reduce the image size or re-sample to show it in smaller screens or as thumbnails so the loading time can be reduced.

The below code will help you generate a copy of the original image in the destination folder and dimensions you specify.


//FUNCTION TO REDUCE IMAGE SIZE
function reduce_image_size($destination,$image_name,$files)
{
//REDUCE IMAGE RESOLUTION
if($files)
{
$dest = $destination.$image_name;
$width = 300; //in pixels
$height = 300;
list($original_width, $original_height) = getimagesize($files);
$original_ratio = $original_width/$original_height;
if ($width/$height > $original_ratio)
{
$width = $height*$original_ratio; //To maintain the aspect ratio
}
else
{
$height = $width/$original_ratio;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($files);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $original_width, $original_height);
imagejpeg($image_p,$dest, 100);
ImageDestroy ($image_p);
}
//END OF REDUCING IMAGE RESOLUTION
}

Codeigniter – Placing assets folder

While many practices are followed while placing the assets folder in Codeigniter, I feel that the best place is in the root folder along with the application directory as shown below.

structure

Edit the .htaccess file as shown below :


RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

You can directly call the assets files in the View pages as shown below :


<link rel="stylesheet" type="text/css" href="assets/css/style.css">

Remove index.php from URL in Codeigniter

Open applications/config/config.php and make the following changes :

Replace


$config['index_page'] = 'index.php';

With


$config['index_page'] = '';

And add the following code to a new file and save it as .htaccess inside the root folder :


RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Add Overlay For A Background Image In CSS

If you are looking for a cross browser solution to add an overlay to a background image with css, look no more. Here I’m adding a black transparent overlay to the header image.


background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(img/header.jpg);
background-image: -moz-linear-gradient(top, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(img/header.jpg);
background-image: -o-linear-gradient(top, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(img/header.jpg);
background-image: -ms-linear-gradient(top, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(img/header.jpg);
background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0, 0, 0, 0.5)), to(rgba(0, 0, 0, 0.5))), url(img/header.jpg);
background-image: -webkit-linear-gradient(top, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(img/header.jpg);

Hope this helps!

How To Capture a Page Title Using PHP

Since we know that the title of the page in html will be inside the title tag, we can use the following regular expression to capture the page title of a page easily.


$title_regex = '%&lt;title&gt;(.+)&lt;\/title&gt;%i';
$page = file_get_contents(urlencode("http://www.google.com"));
$matches = array();
 if (preg_match($title_regex, $page, $matches) && isset($matches[1]))
$title = $matches[1];
else
$title = "Not Found";
echo $title;