Archive for the ‘PHP’ Category.
February 28, 2010, 7:56 PM
Not to long a go we explained how to generate a sitemap.xml with PHP.
Well, there was something missing and I think really important, to ping Google about the changes in the sitemap.
Simplest way of doing this:
file_get_contents('http://www.google.com/webmasters/sitemaps/ping?sitemap=http://yoursite.com/sitemap.xml');Remember to register your site at Google webmaster tools, since if you don’t will not work.
February 26, 2010, 5:20 PM
Imagine that you need to have an installation form for your web app.
Of course you will need to ask many things, but before you ask, what about been sure the client match all the software requirements?
Check the PHP version:
$phpversion = substr(PHP_VERSION, 0, 6);
if($phpversion >= 5.2) {
echo "Right PHP version";
}
else{
echo "No, you can't continue with the installation";
exit;
}In this example we require 5.2 at least to continue.
Check extension loaded in PHP:
if (!extension_loaded('curl')){
echo 'Not found, please proceed to install';
exit;
}
else echo 'Found';In this example we check that CURL it’s loaded.
Checking apache module installed:
if (in_array('mod_rewrite',apache_get_modules())) echo 'Found';
else echo 'Not found';In this example we check that mod_rewrite it’s loaded.
Checking folder permissions:
if(is_writable('/images')) {
echo 'OK - Writable';
}
elseif(!file_exists('/images')) {
echo 'File Not Found';
}
else {
echo 'Unwritable (check permissions, chmod 755 should fix this)';
exit;
}Checking mysql connection:
if (!mysql_connect($_POST["DB_HOST"],$_POST["DB_USER"],$_POST["DB_PASS"])){
$msg=mysql_error();
echo 'Mysql error:' .$msg;
exit;
}
else echo "connected!";February 24, 2010, 2:57 PM
Simple function to read RSS where you can set some values:
function rssReader($url,$maxItems=15,$begin="",$end=""){
$rss = simplexml_load_file($url);
$i=0;
if($rss){
$items = $rss->channel->item;
foreach($items as $item){
if($i==$maxItems) return $out;
else $out.=$begin.'<a href="'.$item->link.'" target="_blank" >'.$item->title.'</a>'.$end;
$i++;
}
}
return $out;
}Usage example:
echo '<ul>'.rssReader('http://neo22s.com/feed/',5,'<li>','</li>').'</ul>';Using cache with the class fileCache:
function rssReader($url,$maxItems=15,$cache,$begin="",$end=""){
$cache= (bool) $cache;
if ($cache){
$cacheRSS= new fileCache();//seconds and path
$out = $cacheRSS->cache($url);//getting values from cache
}else $out=false;
if (!$out) { //no values in cache
$rss = simplexml_load_file($url);
$i=0;
if($rss){
$items = $rss->channel->item;
foreach($items as $item){
if($i==$maxItems){
if ($cache) $cacheRSS->cache($url,$out);//save cache
return $out;
}
else $out.=$begin.'<a href="'.$item->link.'" target="_blank" >'.$item->title.'</a>'.$end;
$i++;
}
}
}
return $out;
}Usage example RSS with cache:
echo '<ul>'.rssReader('http://neo22s.com/feed/',5,true,'<li>','</li>').'</ul>';Based on this one from sihan: Continue reading ‘Read RSS in PHP with cache’ »
January 28, 2010, 12:00 AM
Just a handy function that I use a lot.
With this function you can generate an input select from a given query.
Parameters:
1- Query to be displayed as a select, the query needs to returns at least 2 values, 1st one for the option value, and the 2nd one for the display value.
2-Input Select name, thisw woud be the name that your select will have in the form.
3-Which option it’s selected from the list. I non is set the first one it’s selected.
function sqlOption($sql,$name,$option){//generates a select tag with the values specified on the sql, 2nd parameter name for the combo, , 3rd value selected if there's
global $ocdb;
$result =$ocdb->query($sql);//1 value needs to be the ID, second the Name, if there's more doens't work
$sqloption= "<select name='".$name."' id='".$name."'>
<option value='0'>Home</option>";
while ($row=mysql_fetch_assoc($result))
{
$first=mysql_field_name($result, 0);
$second=mysql_field_name($result, 1);
if ($option==$row[$first]) { $sel="selected=selected";}
$sqloption=$sqloption . "<option ".$sel." value='".$row[$first]."'>" .$row[$second]. "</option>";
$sel="";
}
$sqloption=$sqloption . "</select>";
echo $sqloption;
}Same function but that can group:
Continue reading ‘Input select generated from query – PHP’ »
January 26, 2010, 11:42 PM
Simple code of the day:
function getIp(){//obtain the ip
// if getenv results in something, proxy detected
if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip=getenv('HTTP_X_FORWARDED_FOR');
}
else {// otherwise no proxy detected
$ip=getenv('REMOTE_ADDR');
}
return $ip;
}Anyway of doing this better?