» Smart Phones, Web Design, Google or whatever
Posts tagged php
PHP URI/URL of This Page in Address Bar
Nov 16th
Sometimes PHP_SELF doesn’t do what I need when I’m trying to programmatically find the name of a web page on-the-fly on certain dynamic sites I’ve worked on. for example, I might want to show or hide Adsense, or banners or something based on the page’s name. In these case sometimes this PHP $_SERVER variable works very nicely…
<?php
$_SERVER['REQUEST_URI'];
?>
Here’s an example of typical usage:
<?php
if ($_SERVER['REQUEST_URI'] == “/south-carolina.php”) {$kontera = “hide”;}
?>
… which hides Kontera on this page. Some site I work on use .htaccess and Apache rewrites to make dynamic URLs out of dynamic URLs. In these cases REQUEST_URI can be a life-saver.
Using PHP to redirect to another page
Jun 18th
Using PHP to redirect to another page is a common task, especially when you are working with email forms. Fill the form out, process it, then redirect to a “thank you” page:
<?php
header( “Location: http://www.example.com” ) ;
exit;
?>
You can also use variables for the URL like so:
<?php
$destURL = “http://www.example.com”;
header( “Location: ” . $destURL ) ;
exit;
?>
NOTE: The code above has to be processed before any HTML headers, so it needs to be placed above ANY HTML code.
NOTE 2: Watch the code above, depending upon my current CSS in my current theme the single quotes or even double quote may change to fancy quotes which will need to be edited if you cut-n-paste the code.
MORE info available on PHP.net’s header manual page
Using PHP to grab the Referer
Jun 18th
You can use PHP to track how someone got to a specific page by grabbing the referring page, called the referer. Use this code to get that info:
<?php
$myReferer = $_SERVER['HTTP_REFERER'];
?>
NOTE: Watch the code above, depending upon my current CSS in my current theme the single quotes or even double quote may change to fancy quotes which will need to be edited if you cut-n-paste the code.
MORE info available on PHP.net’s $_SERVER manual page
PHP: Find Server and Domain Paths
Apr 27th
For building sites that can more easily be moved from one server to another, and allows simpler updating of common information like email address and phone number of the client.
Here’s some PHP to generate paths: both server paths and domain paths good for use in all sorts of ways for include and require statements.
<?
// for INCLUDE statements – this is the local server path
$urlPATH = “http://” . $_SERVER['SERVER_NAME'];
$srvPATH = $_SERVER['DOCUMENT_ROOT'];
include ($srvPATH.”/config-settings.php”);
?>
More later.
PHP Do Until – Do While
Apr 16th
The PHP Do While, just a quick post so I can find this later without Googling…
<?
$i = 0;
do
{
// do something
}
while ($i > 0);
i++;
?>
MORE
PHP Do While on PHP.net
PHP Local Time – Your timezone
Dec 11th
UPDATE: Better code. Creds for this updated code to my buddy Seth Furchgott who did the googling and grabbed the following from PHP.net, much better than what I originally posted below.
<?php
date_default_timezone_set(‘America/New_York’);
echo date(‘g:i A’);?>
Documentation for timezones:
http://us3.php.net/manual/en/timezones.php (get time zones here by Continent…and Ocean)
http://us3.php.net/manual/en/timezones.america.php (US timezones)
OLD PHP TIME POSTS (newer PHP timezone post): After a comment from a visitor I re-checked my code, did some testing and added the echo statement with formatted time, and improved the comments about how to check for the correct value of $offset. Also, note I have not used this code for a year yet so I do not know if daylight savings has been taken into account.
PHP code to get time in YOUR timezone…
<?php
$mydatetime = time ();
$offset= 0; // offset in # of hours from desired location
// check the time displayed using $offset = 0 and adjust
// the offset accordingly
$mydatetime = $mydatetime + ($offset * 60 * 60);
echo date (“g:i A”, $mydatetime);?>
Check the PHP Time manual page for formatting the time.
Comments/feedback are invited.
PHP – Do Until Certain Date
Sep 19th
UPDATE: Since I often use this bit of code to do things like display rel=”nofollow” on a link after a certain date, I’ve written a shorter, inline snippett to do the same thing. Keep in mind when copy-n-pasting the code below you may have to change the fancy quotes to regular quotes:
<?php if (date(“Ym”) > 201101) { echo ‘rel=”nofollow”‘;} ?>
I’m not deep into PHP, so I wrote this ‘hack’ to allow HTML or whatever until a certain date. The sample below will do whatever HTML is until a certain date…
<?php
$mymonth = date(“m”);
$myyear = date(“Y”);
$mycompare = $myyear . $mymonth;
// if its before Jan 2010
if ($mycompare < 201001) {?>
<p>Before 2010!</p>
<?php } else { ?>
<p>Its 2010 or later!</p>
<?php } ?>