PHP


9
Aug 07

TinyUrl code

The tinyurl.com page has an example of urls you can use. One of them is tinyurl.com/3 which redirects to a unicycle page. I’d always assumed that they encode the url, in the same way as a zip file is an encoded version of the real file, but I guess they just have a database which keeps track of the codes. So somewhere in that database, there’s a place that says id=3 link=”unicyclepage.com/blah.html”.

It used to be that they were sequential, so if you type in tinyurl.com/4 then tinyurl.com/5 you can see those pages in the order they were input to the system. It’s always a security risk to do this, since you’ll get people who like to make scripts to go through all the pages. And you get people who can predict what the next one will be and mess with it like this.

Yesterday, I needed a way to refer to pages in a database, but didn’t want to use the primary key id (which auto increments) to do it. So I made a script to generate tinyurl style random patterns.

// make a tinyurl.com style id for pages. this one only generates 4 (or more) character
// alphanumerics. there are about 1.6 million, should ben enough for most people.
// it's not fussy about making pottymouth urls, like sex1 or feck, but the chances
// are pretty slim... Oops, I ran it 5 times and got sexx! What're the odds?
// examples: v7or vfbt im0x b7tu 59jx 3a4k a7kw mvg9 supe 1rx9 4ww6 tgpn ezb7 z6h1
// let's see some examples
for ($i=0; $i<14; $i++) {
echo make_tiny(13);
echo " ";
}
function make_tiny ($how_long = 4) {
// make an array with the alphabet
$alphabetnum = array('a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
$return = "";
for ($i=0; $i<$how_long; $i++) {
$char = rand (0, 35);
$return .= $alphabetnum[$char];
}
// could include some checking here, to see that you really have a unique id
// for example, look in a database and compare the ids that are there
// there's a chance that this will get recursive on your ass (if all ids are taken)
// it needs extra checks to avoid an infinite loop
if ($return == "v7or") { $return = make_tiny(); }
return $return;
}


15
Jun 07

MacOS MySQL terminal tip

Desktop ClippingHere’s something that saves me time when I’m starting a new database and still testing PHP scripts which input to the database. I want to clear the database of all information, but don’t want to have to recreate the database structure. I just want a bare bones database with no content, so I can see exactly what information is going in and what information is getting messed up along the way.

I’m working in terminal. I write a series of commands to delete the contents of the tables I want cleared. Instead of pressing return each time, I separate them with a semicolon. Here’s an example:

delete from authors;delete from reports;delete from paper_authors; delete from journals;

This deletes all information from the four tables: authors, reports, paper_authors and journals.

Now I highlight the line, right up to the last semicolon, and drag it to the desktop to make a clipping. Next time I want to clear my db, I drag the clipping from the desktop onto the terminal window and press enter. Automagic.

You can do this with any command you want too, from ftp open commands to long folder names. Drag any folder onto the terminal window and the path shows up, saving lots of stubby fingers.

And for an extra bit of magic, look what happens when you drag a folder on a BBEdit or Textedit window. Useful for folders of mp3s.


6
Jun 06

Hello world!

PHP Basics

Here’s the most basic thing you can do in PHP, print something on the browser screen. The command for doing this is print. So the command to print “hello” is

print “hello”;

with a semicolon at the end. Put this between some PHP markers <?php to start and ?> to end, and you have a php program.

<?php print “Hello”; ?>

You can do it other ways, like putting the word in brackets

<?php print(“Hello”); ?>
and it won’t change the output. you can leave out the semicolon since this is the only statement

<?php print “look, no semicolon” ?>

and you can squish the last php marker really close to the quotes

<?php print “it’s really close!”?>

but you can’t do this

<?phpprint “too close for comfort” ?>

bizarrely, you can do away with the quotes and just use brackets

<?php print(even_this_prints) ?>

and it will still work, but this won’t

<?php print(no spaces please) ?>