Recently I came up with the idea that I could use a Gmail account as a place holder to store pictures sent from my cell phone. The idea was that I could take a picture with my cell phone and it would instantly show up some where, such as a website or blog. The following code can be used to do this fairly easily. Simply create a Gmail account for testing purposes and use the following code to download and view the images from your Gmail account.
<?php
$save_path = "/tmp/";
$server = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$login = "someaddress@gmail.com";
$password="SomePassword12345";
Read more…
December 28th, 2009
Jimmy
If you are planning on moving a website from one domain name to another you’ll probably want to forward all your traffic from your old site. Many people just forward the actual domain name to point to the new domain name but this won’t help people who land on specific pages. If your links stay the same and it’s just a domain name change this can be done with just a few lines of code. If your linking structure has changed, you’ll need to do some additional work. Below is the code for a domain name change where the new site links are the same as the old site links with the obvious exception for the domain name.
This would be a good example of what you could do if you were running a CMS or Blogging system such as PHPNuke or WordPress. This code checks to see which page is being requested and then builds a new link to forward the user to. It also lets any search engines know your page has moved with the 301 header tag.
<?php
$uri = $_SERVER['REQUEST_URI']; //Get the incoming URL request not including the domain name....
header("HTTP/1.1 301 Moved Permanently"); //Tell the requester this page has moved.
header('Location: http://www.new-website.com' . $uri); //Concatenate the requested URL onto our new domain name and forward the requester.
die?>
If your new site contains a different linking structure than your old site you may want to forward your most popular pages to the newly formatted URLs.
<?php
$uri = $_SERVER['REQUEST_URI']; //Get the incoming URL request not including the domain name....
$oldSiteLinks = array("/contacts.html","/products.html","/chatforums/");
$newSiteLinks = array("http://www.newsite.com/aboutus.php","http://www.newsite.com/newproducts.php","http://forums.newsite.com");
//Make sure the two above array are aligned. Each element should be alligned to the corrisponding "oldUrl / newUrl" format.
//Element 0 in oldSiteLinks should forward to element 0 in newSiteLinks!
if(in_array($uri,$oldSiteLinks))
{
//If the requested URL on the old site is one of the links I wanted to forward ($oldSiteLinks) then lets redirect that page...
$idx = array_search($uri, $oldSiteLinks); //get the index for the oldsite link.
$newURL = $newSiteLinks[$idx]; //Get the new link from the corresponding link array.
}else{
//If we don't find a page in the $oldSiteLinks array just forward them to the new domain.
$newUrl = "http://www.new-site.com";
}
header("HTTP/1.1 301 Moved Permanently"); //Tell the requester this page has moved.
header('Location: $newURL); //Redirect the requester to the new URL. The newSiteLinks array items contain the domain name so no concatenation is required.
There are of course more ways you can do this however this has been the easiest method I’ve come up with. Moving to a new domain name and new site all together with all new links is always challenging but with a few minutes of work you can at least forward your popular pages and sections of your old site to your new one.
December 20th, 2009
Jimmy
The following PHP code will display a set of jpeg files from a folder on your webserver. This code can be used to generate a list of thumbnails provided that the thumbnails are already resized. This code simply outputs the images “img” html tag with the “src” already in place. I would recommend putting this inside a “<div>” tag so that you can control the height and width of the images together. Creating CSS for your image can also control the actual size of the image, or you could just add a width or height to the img tag for auto-scaling.
<?
$directory = opendir("/home/jimbob/www/htdocs/mypics");
while ($file = readdir($directory)) {
if (preg_match("/\.jpg/",$file)) {
echo "<img src=\"drawthumb.php?http://somewebsite.com/mypics/$file\" border=1>";
echo "&nbsp;";
}
}
closedir($directory);
?>
December 19th, 2009
Jimmy
If you’re like me, you may once in a while have something to write about that isn’t necessarily related to your overall blog topic. Take me for example. My blog is mainly about internet and application development (programming) however sometimes I find a really cool tip or trick that isn’t necessarily related, such as a howto for example. Using this code I can exclude those posts from my RSS feeds. That way my main RSS subscribers don’t get caught reading off-topic posts, but they are there if they want to read them on the site. I then include just the headlines of those posts on my extended recent posts sidebar widget.
Anyways enough of the blabbing on onto the code. This code excludes categories 35 and 45 from my RSS feeds. You can get the category ID’s on your category admin page in your WordPress dashboard.
add_filter('pre_get_posts', 'filterRSSQuery');
function filterRSSQuery($query) {
if ( $query-&gt;is_feed ) {
$query-&gt;set('cat', '-30,-45');
}
return $query;
}
December 11th, 2009
Jimmy
PHP debug tools provides various debug tools, including: script trace debugging (using the xdebug extension), error debugging, manual debugging using a debug() function that generates a backtrace and other useful information, and debugging database queries.
December 10th, 2009
Jimmy
The odsPhpGenerator has been released. The generator is a small and easy library written in PHP 5.0 to make the creation of Open Document Spreadsheets easy. This first version requires PHp5.0. Below is a simple example of how you would use this library.
// Load library
require_once('ods/ods.php');
// Create Ods object
$ods = new ods();
$ods->setPath2OdsFiles('ods');
// Create table named 'table 1'
$table = new odsTable('table 1');
// Create the first row
$row = new odsTableRow();
// Create and add 2 cell 'Hello' and 'World'
$row->addCell( new odsTableCellString("Hello") );
$row->addCell( new odsTableCellString("World") );
// Attach row to table
$table->addRow($row);
// Attach talble to ods
$ods->addTable($table);
// Download the file
$ods->downloadOdsFile("HelloWorld.ods");
Homepage
Download
December 10th, 2009
Jimmy
Do you need to upload a file to your php web server from a .NET application? I did, and I found that it was actually pretty easy. Basically the upload.php file works the same way as a basic HTML website form. You simply do an HTML form post from within your .NET application.
The source code for the VB.NET application:
Dim WC As New System.Net.WebClient
Dim uriString As String = “http://www.yourwebserver.com/upload.php”
Dim fileName As String = “c:\somefile.txt”
Dim responseArray As Byte() = WC.UploadFile(uriString, “POST”, fileName)
MsgBox(System.Text.Encoding.ASCII.GetString(responseArray))
The source code for the PHP upload.php script:
<?php
$uploaddir = "/home/webspace/htdocs/uploads/";
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)){
echo "File Uploaded!";
}else{
echo "Problem uploading file.";
}
?>
Call the .NET code above to upload a file. Make sure you have a writable directory on your php web server. The application should throw a message box up with the results of upload.php. ( Basically whatever upload.php echo’s back. )
December 10th, 2009
Jimmy
The following code is a basic example of how to use the Yahoo search API. An array of objects is returned containing the search results letting you create dynamic scripts to show images. This code could be used to make a simple plug-in for WordPress to show images based on tags in your posts or you can use it in any PHP script.
<?php
$keyword =”Cool+Wallpaper”;
$request = ‘http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=’ . $keyword . ‘&results=12&output=php’;
$response = file_get_contents($request);
$phpobj = unserialize($response);
foreach($phpobj['ResultSet']['Result'] as $imgobj)
{
echo $imgobj['Thumbnail']['Url']; //The URL for the thumbnail
echo $imgobj['ClickUrl']; //The URL for the full image;
echo $imgobj['Width']; //The full picture width; ( Do I need to post the height example? )
}
?>
This snippet of PHP code will show you how to get a Google page rank for a specific website. The function below will return the page rank as an integer. The code was pulled from the wordpress links directory plugin located here.
Read more…
Here is a PHP script I wrote to backup my mySQL databases. Just use a cron job to run the PHP script when you want. The PHP MySQL database backup script will also email the database. With a Google Gmail account you get more than 7 gigs of storage so they are a good service for using for your backups.
Read more…