<?php
function httpscheck($url = '')
{
if($_SERVER['HTTPS'] != "on" || $_SERVER['HTTP_PORT'] != 443)
{
if(strlen($url) == 0)
{
$url = $_SERVER['REQUEST_URI'];
}
if(headers_sent())
{
trigger_error('SSL redirect failed as HTTP headers were previously sent to the browser', E_NOTICE);
}
else
{
die(header('Location: ' . $url));
}
}
}
Easy way to check if HTTPS / SSL is being loaded (PHP)
October 21, 2011 at 03:55:25 AM / 1 comments / Code, PHP
PHP - simple cookie checking script
<?php
if(isset($_GET['set']) && $_GET['set'] != 'yes')
{
// Set cookie
setcookie('test', 'test', time() + 60);
// Reload page
header("Location: cookie.php?set=yes");
exit ;
}
else
{
// Check if cookie exists
if(isset($_GET['set']) && ! empty($_COOKIE['test']))
{
echo "Cookies are enabled on your browser";
}
else
{
echo "Cookies are <b>NOT</b> enabled on your browser";
}
}
?>
October 21, 2011 at 03:53:16 AM / 1 comments / Code, PHP
cURL error codes PHP array
<?php
$curl_errors = array();
$curl_errors[0] = "CURL_OK";
$curl_errors[1] = "CURL_UNSUPPORTED_PROTOCOL";
$curl_errors[2] = "CURL_FAILED_INIT";
$curl_errors[3] = "CURL_URL_MALFORMAT";
$curl_errors[4] = "CURL_URL_MALFORMAT_USER";
$curl_errors[5] = "CURL_COULDNT_RESOLVE_PROXY";
$curl_errors[6] = "CURL_COULDNT_RESOLVE_HOST";
$curl_errors[7] = "CURL_COULDNT_CONNECT";
$curl_errors[8] = "CURL_FTP_WEIRD_SERVER_REPLY";
?>
October 21, 2011 at 03:51:03 AM / 1 comments / Code, PHP
prevent browser caching with PHP
<?php
function bustcache()
{
if(headers_sent())
{
trigger_error('Already sent HTTP headers, not busting the cache', E_NOTICE);
}
else
{
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
}
}
?>
The above is a very simple function for preventing a browser from caching content. This isn't recommended on static sites, but is exceptionally helpful when building dynamic applications. Simply call this function before any output.
October 21, 2011 at 03:48:06 AM / 1 comments / Code, General Web Programming, PHP
how to fix the broken edit button in shutter
Shutter is one of my favorite Linux programs. It does what Windows' Snipping Tool does, plus a lot more.
One of the functions I really like is being able to edit screen shots; annotation, arrows, circles, whatever you need to illustrate the screenshot.
Recently I upgraded a machine to Ubuntu 10.04 and found that the edit button was grayed out --- Shutter would no longer let me edit any screenshots.
Thankfully, the fix was simple:
sudo apt-get install libgoo-canvas-perl
October 18, 2011 at 05:42:16 AM / 1 comments / Linux, Ubuntu
ways to make your website easier to maintain using php
This is nowhere near an all inclusive list of things you can do to make your life easier when having to maintain a website. These are just a few tips and tricks worth knowing.
1) Never use absolute paths. Use relative paths instead.
For example, this is a no-no:
include('/home/user/public_html/some/file');
Instead, try this:
include($_SERVER['DOCUMENT_ROOT'] . '/some/file');
Or even this:
include('../../some/file');
Then if (when!) you migrate to a new server, and your base web dir changes, you won't have to update your file paths throughout your code.
2) Use a config file.
Don't hard code your email address throughout your site. define() it in a config file that you include() everywhere. Same goes for mysql credentials, or any other text which may need to change at some point. Putting everything in an external config makes a migration much easier.
Example:
define('EMAIL','my@email.com');
define('DB_USER','mydbusername');
define('DB_PASS','mydbpassword');
Or, use global variables:
$GLOBALS['EMAIL'] = 'my@email.com';
$GLOBALS['DB_USER'] = 'mydbusername';
$GLOBALS['DB_PASS'] = 'mydbpassword';
3) Place re-used web page code into external files. include() them.
Got a navigation menu that appears on every page? Place it into a file (e.g. navigation.php) and include() it on every page.
Google Analytics? Same deal--- include() it.
Doing so makes site maintenance MUCH easier.
October 10, 2011 at 08:22:47 AM / 1 comments / General Web Programming, Perl, PHP
How to tell if PHP is running from the command line (CLI) or the web.
if(isset($_SERVER['REMOTE_ADDR']))
{
die();
}
If you have a script that needs to run from the command line (say, via cron), but for some dumb reason needs to reside in a publicly accessible location (e.g. via the web), use the above code at the very start of your script. The only time that particular variable exists is when someone is connecting over the web. Command line execution doesn't create it.
October 6, 2011 at 10:26:43 PM / 2 comments / PHP
How To: Generate a random pause delay in your PHP script
If you have a script that requires a random pause delay --- for example, perhaps a web scraper --- here's a super simple way to do it.
usleep(rand(1000,3000));
The rand() function is being fed two numbers; the lowest number to generate, and the highest.
The usleep() function causes a script to pause for a specified number of microseconds.
So the code above is saying "randomly sleep between 1000-3000 microseconds", which means 1-3 actual seconds.
Simple, and effective!
October 6, 2011 at 02:02:59 AM / 1 comments / General Web Programming, PHP
How To: Generate a random number in Javascript
This is one I forget all the time, so I figure I may as well document it here.
var randomnumber=Math.floor(Math.random()*1001)
"1001" means any number between 0 and 1000 will be generated. So for instance, change it to 101 if you want to use 0-100 as a range. And so forth.
October 6, 2011 at 01:59:11 AM / 1 comments / General Web Programming, Javascript
Ubuntu Screensavers
Ubuntu comes with some pretty decent screensavers.
If you want even more --- alot more -- install the 'xscreensaver-data-extra' package.
For example, from the command line, type:
sudo apt-get install xscreensaver-data-extra
Enjoy!
October 4, 2011 at 11:22:44 AM / 1 comments / Linux, Ubuntu
How To: Run a linux system command at startup using cron
If you have a command that you need run at startup, here's a real simple way to do it.
Simply add this to your crontab:
@reboot /path/to/command
Now the next time you reboot your machine, the command will always be run for you.
October 1, 2011 at 08:51:02 PM / 1 comments / Linux