Archive for the ‘PHP’ category

Easy PHP Caching

July 21st, 2010

Just as the title says, easy php caching.

Step 1: Create a folder in the root of your website to store the cached files and make sure the folder is writable by php. My folder is named appropriately cache.

Step 2: Start the part of the page you want cached with….

// Start Cache
$cachefile = ‘cache/’.basename($_SERVER['REQUEST_URI']); //change cache to whatever you named your folder
$cachetime = 2880 * 60; // Cache for 48 hours before replacing. Change the 2880 to what ever value you want. The formula is hours * 60
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() – $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "“; //you can comment out this line if you don’t want the cache info to show in your html code
exit;
}
ob_start(); // start the output buffer
//start your code here

Step 3: End the part of the page you want cached with….

//end your code
$fp = fopen($cachefile, ‘w’); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser

Sending Text Messages Via PHP

August 26th, 2009

Sunday is family day for us. We try on most Sundays to get together for lunch after church. It is nice to get together and catchup on what is happening in everyone’s lives.

The problem is it is so hard to get everyone to agree on a restaurant! Honestly this is usually a 15 to 20 minute ordeal. Lots of I don’t care, whatever you want, anything but that kind of comments.

The solution, write down all the restaurants within a 5 mile radius that everyone can tolerate, then either go down the list or choose randomly.

Below is a fun little script that I wrote to choose a random restaurant and text the results too us.

Most of the cell phone carriers allow sending of text messages via email Verizon’s format is thenumber@vtext.com, other carriers you will have to check.

set_time_limit(0);

//Creating Restaurants List
$restaurants = array();
$restaurants = array("Applebees", "Red Lobster", "Red Robin", "Chilis", "Sweet Tomatos", "Culvers", "Olive Garden", "Macayos", "Rio Marage", "Golden Corral", "Brookside", "Chipolte", "My Greek Corner", "Fridays", "Paridise Bakery", "Chuy's", "Streets of New York", "Taco Bell", "Charleys", "Wendy's", "Long John Silvers", "Out Back", "Buffalo Wild Wings", "Burger King", "A&W KFC", "NYPD Pizza");

//Selecting Random Restaurant
$rand_restaurant = array_rand($restaurants,1);

//Creating Email List
$emailaddress = array();
$emailaddress = array("1111111111@vtext.com,2222222222@vtext.com");

//Setting Email Properties
$emailsubject = "Today we are eating at....";
$emailbody = $restaurants[$rand_restaurant];
$fromaddress = "you@youremail.com";

//Getting email Count
$i = count($emailaddress);

$z = 0;

//Loop only if we have some email addresses
if ($i != 0)

{

while ($i != $z)

{
// Send the Mail
mail($emailaddress[$z], $emailsubject, $emailbody, "From: " .$fromaddress. "\nX-Mailer: PHP 4.x");

echo $z + 1 . " out of " . $i . " emails sent. (" . $emailaddress[$z] . ")
";

++$z;

}

}

else

{

echo "Warning: No emails in array.";
}

Also I wanted the text to be sent out every Sunday at 10:05AM so I added the following line to /etc/crontab

5 10 * * sun php /path/to/file.php