Your open source for Development and Design

Shorthand boolean IF with PHP

Filed under: PHP — Tags: , , — Adi @ 1:39 pm February 4, 2009

This is a good alternative to using a basic IF condition. It basically fits the whole thing on a minimal line of code.

It is anĀ  easy way to compare two values and select the right one. There are a few ways to use this but the basic jist of it is to compare two values and if its is true use the first value after the question mark, or if the condition returns false use the second value.

echo $targetvar =  ($somevar == "thisVar" ? "itIsTrue" : "itIsFalse");


In the example above we can set $targetvar with the condition. it will compare the first two variables with whatever you use, more then less then etc and it will choose the correct value from the two after the question mark.

SEO Friendly Redirects with PHP

Filed under: PHP,SEO — Tags: , , — Adi @ 1:32 pm January 24, 2009

With a basic redesign you probably wont have this problem but if you have rebuilt a website even with the same content if the URL’s have changed this will have a huge effect on your search engine presence as you will be starting from scratch. Your links from internal pages may not work, links from external sites wont work and you will lose your pagerank (if it ever mattered anyway.)

A good way to keep your sites listing after a redevelopment is to use a redirect. If you are using an Apache server you can use the .htaccess file to redirect.

Redirect 301 /oldpage.html /newpage.html

But some times this isnt the best way if you have to redirect hundreds of pages. The alternative is to use a header redirect in PHP. Most web based languages such as ASP or RUBY will do the same thing but with different code.


<?php

// make this code the first line
header("Location: /newpage.php",TRUE,301);
// 301 Moved Permanently to newpage.php
?>

Tracking visitors with PHP and Predefined variables

Filed under: Apache,PHP — Tags: , , , — Adi @ 4:37 pm January 15, 2009

Although Google analytics can tell us a lot about who is visiting our websites it is still flawed in many ways. Google analytics is a powerful tool but if the user has disabled Javascript or you want to know live stats it can be pretty useless.

Developing our own can be much more versatile as we can gather information straight from the server using PHP’s Predefined variables.

You can add your own version of with a database and a few lines of PHP code.

$time = mktime();
if (isset($_SERVER['REQUEST_METHOD'])) {   // HTTP-method
   $method = $_SERVER['REQUEST_METHOD'];
} else {
   $method = "";
}
if (isset($_SERVER['REMOTE_ADDR'])) {      // IP-adress
   $ip_adress = $_SERVER['REMOTE_ADDR'];
} else {
   $ip_adress = "";
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {  // Browser
   $browser = $_SERVER['HTTP_USER_AGENT'];
} else {
   $browser = "";
}
if (isset($_SERVER['PHP_SELF'])) {         // Current page
   $page = $_SERVER['PHP_SELF'];
} else {
   $page = "";
}
if (isset($_SERVER['HTTP_REFERER'])) {    // Previous page
   $referer = $_SERVER['HTTP_REFERER'];
} else {
   $referer = "";
}

$sql="INSERT INTO user_log (time, method, page, referer, adress, browser) // Insert into database as new row
					VALUES('$time','$method','$page','$referer','$ip_adress','$browser')";
mysql_query($sql);

« Newer PostsOlder Posts »

Switch to our mobile site