WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Jump Start to Easy URLs
Categories : PHP, Beginner Guides, MySQL, File System, To PHP
Keith Reichley
Keith Reichley
Date : 2001-01-26
Grade : 0 of 5 (graded 0 times)
Viewed : 21157
Search : More Articles by Keith Reichley
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


You've probably seen those short and sweet URLs that "jump" or "go" somewhere. In my early days (last year) as a PHP coder, webmaster of a new PHP/mySQL-based site and e-zine editor, I noticed more and more of these jump urls. They got my attention because they seemed like an easy way to:



  • Reduce the length of especially long URLs
  • "Hide" the identity of a link

The former item was especially critical when I wanted to display a newsletter link to information that would be of great interest to my readers, but the length of the URL exceeded the standard 65-character limit of standard text newsletter lines. A URL that breaks into a second line of text is virtually useless to the reader -- it's not clickable "as is", and it requires one or more copy-and-paste operations to get it into the browser's address line.



For example, I have a page that's deeply buried in my site's structure. Because of this, I want to replace its actual URL:





http://www.webthejoint.com/somedirectory/archivedirectory/2000/december/index.html



with something like this:



http://www.webthejoint.com/jump.php?id=5



Luckily for me, I use PHP and mySQL. If you do, too, here's an easy solution that you can cobble together quickly.



Start by creating a simple mySQL table called "redirects". If your hosting service doesn't provide a front-end for mySQL table creation, execute an SQL statement like this:


CREATE TABLE redirects (
id mediumint(8) unsigned NOT NULL auto_increment,
address varchar(200) NOT NULL,
label varchar(60) NOT NULL,
PRIMARY KEY (id),
KEY id (id, address),
UNIQUE id_2 (id)
);


Once the table has been created, add records to this table. The fully-qualified URL gets entered into the address column, a description (for documentation purposes) in the label column. No need to fill in the id column -- mySQL automatically assigns this number.



For the above link, my record would have the following column values:

address:
http://www.webthejoint.com/somedirectory/archivedirectory/2000/december/index.html

label: Archive list for Dec. 2000 newsletters

Assuming this was the fifth record I put into the table, the URL I would use would be the much simpler:

http://www.webthejoint.com/jump.php?id=5

As you may have guessed, the final piece of this puzzle is a PHP program called "jump".

Copy this code and save as "jump.php" into your root directory. Remove the line counters before use.


~~~~~~~~~~~ ( begin code ) ~~~~~~~~~~~
1. <?
2. $db_name
= "yourdatabasename";
3. $table_name = "redirects";
4. $pathstring = getenv("HTTP_HOST") == "localhost" ?
getenv("PATH_INFO") : $PHP_SELF ;

5. $id = $id;

6. $connection = mysql_connect("localhost", "yourdatabasename", "yourpassword")
7. or die("Couldn't connect.");

8. $db = mysql_select_db($db_name, $connection)
or die(
"Couldn't select database.");

9. $sql = "SELECT id, address FROM $table_name";
10. $sql .= " WHERE id = " . $id . ";";

11. $result = mysql_query($sql,$connection) or die("Couldn't execute query.");

12. while ($row = @mysql_fetch_array($result))
13. {
14. $id = $row["id"];
15. $address = $row["address"];
16. }

17. print "\n";
18. print "\n";
23. print "\n";
26. print "\n";
27. print "\n";
28. print "Click here to continue.\n";
29. print "\n";
30. print "\n";
31. ?>
~~~~~~~~~~~ ( end code ) ~~~~~~~~~~~


Now our example link http://www.webthejoint.com/jump.php?id=5) takes the visitor to our "buried" page. The jump program does a lookup to the redirects table in mySQL and returns the record whose id value is "5". The program then builds the HTML and Javascript code, dropping the address value into location.replace method on line 20 as well as the META tag on line 24 and the anchor tag on line 28.



Generally I use "straight" links on our site at WEBtheJOINT, but when I find the need to shorten a long URL or perhaps "disguise" the actual link, all I need to do is add a record to my redirects table and note the id number that mySQL has assigned to the record.









Beginners guide to PHP and MySQL - Creating a simple guest book
Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL
Counting - Creating a simple counter
Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL
Counting - Creating a GIF based counter using PHP and MySQL
Categories : Beginner Guides, PHP, To PHP, To MySQL, MySQL
Counting - Creating a more sophisticated GIF based counter using PHP and MySQL
Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Who's Linking?
Categories : PHP, Beginner Guides, To PHP
Grabb'n and Pars'n
Categories : Beginner Guides, PHP, To PHP
How TO Install PHP, Apache and MySQL on Linux or Unix
Categories : PHP, MySQL, Apache, Installation, Beginner Guides
Beginners Guide to PHP - Introduction to cookies
Categories : Beginner Guides, Cookies, To PHP, PHP
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
Start Using MySQL
Categories : MySQL, Databases, To MySQL, Beginner Guides
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
Referer Statistics
Categories : PHP, MySQL, HTTP, Databases
Derek Adair wrote : 226
No success in showing my id=1 page. Page comes up
with only `Click here to continue` on it. Any help for a
newbie to MySQL & php would be much appreciated.
Anonymous wrote : 227
missing lines in the jump.php3 code lines number 19-22
and 24
Keith Reichley wrote : 228
Here`s the code again, this time including the
missing lines:

<pre>
~~~~~~~~~~~ ( begin code ) ~~~~~~~~~~~
1. <?
2. $db_name = "yourdatabasename";
3. $table_name = "redirects";
4. $pathstring = getenv("HTTP_HOST")
== "localhost" ?
getenv("PATH_INFO") : $PHP_SELF ;

5. $id = $id;

6. $connection = mysql_connect
("localhost", "yourdatabasename",
"yourpassword")
7. or die("Couldn`t connect.");

8. $db = mysql_select_db($db_name, $connection)
or die("Couldn`t select database.");

9. $sql = "SELECT id, address FROM $table_name";
10. $sql .= " WHERE id = " . $id . ";";

11. $result = mysql_query($sql,$connection) or die
("Couldn`t
execute query.");

12. while ($row = @mysql_fetch_array($result))
13. {
14. $id = $row["id"];
15. $address = $row["address"];
16. }

17. print "<HTML><HEAD>\n";
18. print "<SCRIPT language=\"JavaScript1.1
\">\n";
19. print "<!--\n";
20. print "location.replace(\"".$address."\");\n";
21. print "//-->\n";
22. print "</SCRIPT>\n";
23. print "<NOSCRIPT>\n";
24. print "<META http-equiv=\"Refresh\"
content=\"0;
URL=".$address."\">\n";
25. print "</NOSCRIPT>\n";
26. print "</HEAD>\n";
27. print "<BODY>\n";
28. print "Click <A
href=\"".$address."\">here</A> to
continue.\n";
29. print "</BODY>\n";
30. print "</HTML>\n";
31. ?>
~~~~~~~~~~~ ( end code ) ~~~~~~~~~~~
</pre>
Scott Gehret wrote : 266
What can you do if your web host does not have
MySQL or any other database?

Thanks,
Scott
David Lee wrote : 274
Is there a way to make this work if the ISP does not support PHP but supports Perl? (Or is that just an off the
wall question?)
Anonymous wrote : 301
How about using this line of code instead of javascript?

header("location: $address");
Keith Reichley wrote : 302
I agree with you, Jack. Using the PHP header command
is a lot cleaner than going through all that Javascript.
What was I thinking?
Kevin O'Neil wrote : 320
Could you list the whole code with the header:
location: $address part in?

I keep getting an error and can`t fix
Keith Reichley wrote : 321
Kevin -

Try this (without the line numbers, of course):

1 <?php
2 $db_name = "yourdatabasename";
3 $table_name = "redirects";
4
5 $cnx = mysql_connect
("localhost", "dbname", "pwd")
6 or die("Couldn`t connect.");
7
8 $db = mysql_select_db($db_name, $cnx)
9 or die("Couldn`t select database.");
10
11 $sql = "SELECT id, address FROM $table_name
12 WHERE id = `$id`";
13
14 $result = mysql_query($sql,$cnx)
15 or die("Couldn`t execute query.");
16
17 while ($row = @mysql_fetch_array($result)) {
18 $id = $row["id"];
19 $address = $row["address"];
20 }
21
22 header("location: $address");
23 ?>
come on get real wrote : 331
what do I fill in for localhost on these two lines:

$pathstring = getenv("HTTP_HOST") == "localhost" ?
getenv("PATH_INFO") : $PHP_SELF ;

$connection = mysql_connect
("localhost", "databasename", "password")
or die("Couldn`t connect.");
TrickZ i wrote : 353
[new2php]
Hi,
thx for the script.. looks very nice.
I managed to create the tables and stuff and got the php to
work.
But I would like to change it a little bit.
Can anyone show me wich lines I need to remove/edit in
order to get rid of the "click here to continue" part?
So that it goes straigt to the requested page.
thx for any suggestions you might have

,Trickz
Rimas Labrencas wrote : 367
It does not work for me :(.
I could not extract
from "http://www.webthejoint.com/jump.php?id=5" id.
This is how I extracted and tested:
<?php
$id=$_REQUEST[`id`];
print $id;
>
I thought, that this code is responsible for variable extraction:
$pathstring = getenv("HTTP_HOST") == "localhost" ?
getenv("PATH_INFO") : $PHP_SELF ;
$id = $id;

Do you have any other suggestions or explanations of the code
provided?
John Paul Veneracion wrote : 378
thanks for the code! =)
i managed to insert it into the wapsite that i am developing...
oh by the way i made a few modifications for the code...
instead of the "click here" thingy, i just inserted the code:
include ("$address");
into the jump.php file so that once you click a link it goes
directly to the page... =)
thanks man!
btw, how do i make a simple message board for wap?
(different topic...!:)
John Paul Veneracion wrote : 379
1. <?
2. include("../inc.inc");
3. $table_name = "location";
4. $pathstring = getenv("HTTP_HOST") == "localhost" ? getenv
("PATH_INFO") : $address ;
5. $id = $id;
6. $cnx = mysql_connect
("$hostname", "$username", "$password") or die("Couldn`t
connect.");
7. $db = mysql_select_db($db_name, $cnx) or die("Couldn`t
select database.");
8. $sql = "SELECT id, address, label FROM $table_name";
9. $sql .= " WHERE id = " . $id . ";";
10. $result = mysql_query($sql,$cnx) or die ("Couldn`t execute
query.");
11. while ($row = @mysql_fetch_array($result))
12. {
13. $id = $row["id"];
14. $address = $row["address"];
15. $comment = $row["label"];
16. }
17. print "<WML>\n";
18. print "<SCRIPT language=\"JavaScript1.1 \">\n";
19. print "<!--\n";
20. print "location.replace(\"".$address."\");\n";
21. print "location.replace(\"".$comment."\");\n";
22. print "//-->\n";
23. print "</SCRIPT>\n";
24. print "<NOSCRIPT>\n";
25. print "<META http-equiv=\"Refresh\"
content=\"5;URL=".$address."\">\n";
26. print "</NOSCRIPT>\n";
27. print "<card id=\"jump\" title=\"Redirect\"
ontimer=\".$address.\">\n";
28. print `<timer value="50"/>`;
29. print "<center>";
30. include ("$address");
31. print "</center>";
32. print "</card>\n";
34. print "</WML>\n";
35. ?>

here is my code to directly go to the page after calling
jump.php...
why does the meta-equiv-refresh not work? it was supposed to
wait for a set amount of seconds before going to the desired
page right?

thanks!