>>MarcusLingl.net/tech

VersionTracker


The last 10 Mac OSX updates


» Click'n View - 1.3

» Baseball Statbook - 1.3.1

» Chirp - 2.1.3

» Album Cover Finder - 3.4.9

» Calico Panorama - 0.7

» TubeTwist - 1.0

» RecoverTrasher - 1.1

» Mummy Maze Deluxe - 1.1.1

» Mopis - 1.2

» VertiBevel - 2.0





MacSlash


Headlines






Mac OS X Hints


The latest hints


» Omit photos with a given keyword in iPhoto 5

» Use folder aliases for data burn in Toast Titanium

» An AppleScript to open iTunes videos in QuickTime

» Save search results in 10.3

» Set up a Belkin wireless print server

» URLwell - Temporary URL storage via the menubar

» Manage an iPod Shuffle via the Finder and python

» Archive Mail.app email with Gmail

» Lossless adjusting of loudness for MP3 and AAC files

» Add firewall rules via the Terminal





Sourceforge


Last 5 releases


» Unstable 2.0.10-RC1 released (gramps)

» seashore code Seashore 0.1.8 released (The Seashore Project)

» Roundtable ROUNDTABLE SEASON1 CD04.ISO released (Osprey Networks - Free Radio Content)

» Roundtable ROUNDTABLE SEASON1 CD03.ISO released (Osprey Networks - Free Radio Content)

» seashore binaries Seashore 0.1.8 released (The Seashore Project)




Right-click This!

Archives: March 2004

PHP and register_globals

PHP

Mar 14, 04 | 3:15 pm

Good thing I have a lot of hair, because I was pulling a lot of it out today. Last year I threw together a small web based software inventory script using PHP and MySQL. When I upgraded my system to OS X Panther (which uses PHP v4.3.x), the script suddenly broke, making the web interface unusable. I offloaded the database and script to a Linux box (running PHP version 4.2.x), where it has happily been running ever since.

Today I decided it was time to fix the problem so that I could put the database back on it's rightful home, my Mac box. After some rather frustrating time spent troubleshooting, I discovered that forms were no longer passing variables to other pages.

I have a script that follows this logic:


<?php
echo "<form method=\"post\" action=\"result.php\">
<input type=\"text\" name=\"text\" size=\"30\">
<input type=\"submit\" name=\"submit\" value=\"go\">
</form>";
?>


The form calls a file called result.php, which merely contains:


<?php echo $text; ?>


Normally, the result.php page would print whatever was entered into the textbox of the first page. Not the case with PHP 4.3+ (which ships with OS X Panther), with which you get bubcus, nada, nothing. After fiddeling around for a while, I ended up digging into my php configuration file (php.ini), in which I found this most helpful statement:

"; Whether or not to register the EGPCS variables as global variables. You may
; want to turn this off if you don't want to clutter your scripts' global scope
; with user data. This makes most sense when coupled with track_vars - in which
; case you can access all of the GPC variables through the $HTTP_*_VARS[],
; variables.
;
; You should do your best to write your scripts so that they do not require
; register_globals to be on; Using form variables as globals can easily lead
; to possible security problems, if the code is not very well thought of.
register_globals = Off
"

This explained why my pages are no longer passing variables. With register_globals being Off, variables are only valid on the page that defines them. Something else that caught my eye though, was the line "you can access all of the GPC variables through the $HTTP_*_VARS[] variables".

Wanting to know more about the $HTTP variables led me on what would become another long, but most informative learning journey. I headed to the definitive source on PHP, PHP.net, where I found a plethora of additional HTTP variables available, including the variables I was after. PHP's web site also produced another gem, which explained why I had my variable-passing problem in the first place:

"...before PHP 4.2.0 the default value for register_globals was on. And, in PHP 3 it was always on. The PHP community is encouraging all to not rely on this directive as it's preferred to assume it's off and code accordingly."

Sure enough, everything worked as expected if I replaced:


<?php echo $text; ?>


with


<?php echo $HTTP_POST_VARS['text']; ?>


Now knowing my problem but being too lazy to change my script right this moment, disregarding the warning and throwing caution to the wind, I set the register_globals value to On for the time being. Restarted Apache to make PHP reparse the php.ini file and my little inventory script was all happy again.

Well, if I can save even one person just one strand of hair, this post was all worth it. All's well that ends well.

(I make extensive use of the PHP manual at the PHP.net website. So extensively, that I decided to download their HTML version of the manual and put it on my site (here). Usually, when I mention a function, I will link it to the PHP.net web site, however, occasionally I may link it to the manual I have here locally (Why?...because I CAN). If I link to my local pages, I will denote the link with an ML in superscript, like this: date functionML.)

</marcus>

 |  link



Britney Calendar Image

PHP

Mar 09, 04 | 11:06 pm

On my other blog, I implemented a super-quick script to display the current image that coincides with my Britney Spears calendar right above the update calendar.

First things first, I had to scan all 12 images from the calendar. I saved each image with it's corresponding three letter month name (i.e., Jan, Feb, etc) and the extension .jpg.

Then, at the top of the page, I assign the current three letter month name to a variable called $calmonth, by including this line:


<?php $calmonth=date(M); ?>


Using the PHP date functionML with M produces the three letter month name.

Wherever I want my Britney image to appear, I put the following:


<img src="path/to/image/directory/<?php echo $calmonth; ?>.jpg">


Whola! Britney all year-round!

Resources:
PHP Date Function


</marcus>

 |  link



Display Random Photos From Photo Gallery

Howto

Mar 06, 04 | 1:28 am

It's funny how one thing can lead someone off onto another tanget. I was reading the Mac OS X Tip-of-the-Day at OSXFAQ, where the topic of discussion was using the *nix find command to build a directory listing. For some reason, I got the idea to use this featured tip to display random images from my Photo Gallery on various pages throughout the site.

Here's what I've come up with.

First, I would need to get a listing of all the photos within my albums directory. Gallery stores different sizes of each image within the albums directory. All thumbnail images have the .thumb.jpg extension and middle sized ones have the .sized.jpg extension. I want to display the thumbnail images, so I was after all images named imagename.sized.jpg. I navigated to one directory above the albums directory and ran this command in the Terminal:


find albums -name '*.thumb.jpg' > ~/Desktop/thumbs.txt


By putting albums right after find, I am telling the find command to look recursively into the albums directory only. The -name switch directs it to match the expression that follows, in this case *.thumb.jpg, meaning any filename with .thumb.jpg in it. The > sign directs the output to whatever follows, which in this case is a file called thumbs.txt located on my desktop. The resulting file has entries that look like this:


albums/wheeler/DSC00073.thumb.jpg
albums/wheeler/DSC00074.thumb.jpg
albums/wheeler/DSC00075.thumb.jpg
albums/wheeler/DSC00076.thumb.jpg


Now that I have a file with the paths to all thumbnail images within my albums folder, I need to parse this file and randomly select a path. For this task, I turned to PHP.

This script is written as a function so that I could easily place it any page without much typing. I saved it to a file called randimg.php.


<?php
function randpic($picfile) {
$pic_array = file($picfile);
mt_srand ((double) microtime()* 100000);
$randimg = $pic_array[mt_rand (0, count($pic_array)-1)];
echo "<img src=\"/$randimg\" alt=\"Random Picture Gallery\" border=\"1\">";}

?>


$pic_array = file($picfile); -//This reads the file we created into an array called $pic_array. If we could see it, it would look like this:

$pic_array[0]="albums/wheeler/DSC00073.thumb.jpg";
$pic_array[1]="albums/wheeler/DSC00074.thumb.jpg";
$pic_array[2]="albums/wheeler/DSC00075.thumb.jpg";
...etc,

mt_srand ((double) microtime()* 100000); -//This line seeds the random number generator.

$randimg = $pic_array[mt_rand (0, count($pic_array)-1)]; -//This is where our random path is assigned to the variable $randimg. $randimg has to equal $pic_array[somenumber], so we call up the randomizer mt_rand. mt_rand needs to know the first and last numbers it can pick from, so we specify 0 (the first possible variable, $pic_array[0]), and our last possible number. The ending number is calculated by counting the number of lines in the pic_array and then subtracting 1, since we start with 0.

echo "<img src=\"/$randimg\" alt=\"Random Picture Gallery\" border=\"1\">"; -//This line spits out the line of code to display the random image.

Upload both files to your web site (pMachine users may just want to upload the randimg.php into the scripts folder). On each page you want to display random images, simply paste the following tags:

Place this tag near the beginning of your page (disregard if you placed this into your pMachine scripts folder)


<?php include("path/to/your/randimg.php/file"); ?>


...and then wherever you want the random image to appear


<?php randimg("path/to/your/thumbs.txt/file"); ?>


An example of this script can be seen in action on the main Photo Gallery page.

Five whole lines. Seems easy enough, but it took me like 2 hours to cobble it together. At least it was a learning experience, and that's what really matters.

Resources:
OSXFAQ
PHP Web Site

</marcus>

 |  link







 

Links/Nav


Categories

» Apple (0)
» Gallery (0)
» Howto (2)
» Linux (0)
» MacOSX (6)
» MacXM (0)
» Misc Other (0)
» PHP (3)
» pMachine (2)
» Shareware (0)
» Tutorial (0)


Previously Posted

The Archives

Search The Archives

Stuff...

Links



 
 

Powered by:

Powered by pMachine

MySQL

PHP


.Mac