Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post WTF
Today 01:08 PM
Last post by Kyo
Today 01:36 PM
1 Replies, 11 Views
Go to first new post This is a pretty crazy...
Today 12:35 AM
Last post by Axed
Today 01:32 PM
1 Replies, 11 Views
Go to first new post Let us play pokemon!
Yesterday 03:57 AM
by Kyo
Last post by Kyo
Today 01:32 PM
83 Replies, 414 Views
Go to first new post Account Information...
Yesterday 08:08 PM
Last post by jackle56
Today 01:27 PM
1 Replies, 37 Views
Go to first new post Padooshiliana?
Today 12:46 PM
Last post by Snakebite
Today 01:23 PM
16 Replies, 75 Views
Reply
 
LinkBack Thread Tools Display Modes

 Short URL Script (PHP + HTML)
Old 10-18-2006, 03:53 PM   #1 (permalink)
Oosband
Guest

 
Posts: n/a
iTrader: / %
Short URL Script (PHP + HTML)

Short URL Script
First off...the complete script requires a few things:
Linux Web Hosting.
1 MySQL Database.
"public_html" folder allowed to be chmoded to 777.

If any of those 3 things are not possible/not available...then the script will not work on your hosting space.

---

Right, Short URL Scripts are used to create shorter urls for people to use.
For example:
If I wanted to show off my site: http://www.sub-domain.a-domain.com/d.../myphppage.php but I thought the url was too long for people to ever remember, I would simply place my url into a short url script and it will create a shorter url, for example http://www.7ds.us/49. That 7ds.us link would then redirect to my original long url.

---

Step One:
I suppose we're going to want to create the MySQL Database...

Log in to your cPanel. (The following steps may differ depending on what version of cPanel you use, I am writing this based off cPanel 10.8.1).
Now, go to the MySQL Databases section, add a database...probably best to call it something similar to your domain name.
Once you have added that, you have to add a User, shouldn't be too hard...

Now add the user to the database, with Privileges set to ALL.

Once the user is added, click the phpMyAdmin link at the bottom of the page (may be different for different cPanels).

On the left of phpMyAdmin, there is a drop-down menu...on that select the database you have just created.

On the right (in the main box), a set of tabs should have appeared above the main content, click the SQL tab.
Now in the text box, copy this into it:

Code:
CREATE TABLE `url` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`url` text NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM;
Now press the Go button.
That will create a table in the database (url).

---

Step Two:
Now that we have all the MySQL stuff out the way...we're going to set up a file that will connect with the database.

Open Notepad (or any other html editor) and copy in this code:

Code:
<?
$server = "localhost";
$username = "user";
$password = "pass";
$database = "user_db";
?>
Explanation:
$server = "localhost"; is usually set at localhost but may be different for your server.
$username = "user"; change user to the username that you added to your database.
$password = "pass"; replace pass with the password you set with your user.
$database = "user_db"; replace user_db with the name of that database.

Save that file as "config.php".

---

Step 3:
Now we're going to create a page that will list all of the short urls that have been processed by the script, this is mainly for admin use...but you can always link to it if you want.

Save the following as "viewall.php"
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>yourdomain.com</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>

<body>
<?
echo "Database Output

";

include("config.php");
mysql_connect($server,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM url";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$url=mysql_result($result,$i,"url");

echo "www.yourdomain.com/$id - $url
";

$i++;
}
?>
</body>
</html>
Obviously change yourdomain.com with your actual domain...

---

Step Four:
Before we create the index page, I think it would be a good idea to make it look good using CSS. If you need help with CSS, don't hesitate to view the CSS tutorials on this site.

Save the following as stylesheet.css:
Code:
/* CSS Document */

body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
background-color:#EBEBEB;
color: #000000;
}

div {
padding: 1px;
}

a:link { color: #004E82; text-decoration:none; }
a:visited { color: #004E82; text-decoration:none; }
a:hover { color: #E9592F; text-decoration:underline; }
a:active { color: #004E82; text-decoration:none; }

div.header {
border-left:1px solid #000000;
border-right:1px solid #000000;
border-top:1px solid #000000;
border-bottom:1px solid #000000;
width:40%;
font-size:18px;
}

div.inside {
background-color: #CCCCFF;
color: black;
}

div.main {
border-left:1px solid #000000;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
width:40%;
font-size:11px;
border-top:none;
vertical-align:middle;
padding-bottom:0px;
padding-top: 8px;
}

div.footer {
border-left:1px solid #000000;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
width:40%;
font-size:9px;
border-top:none;
}
Feel free to edit all the hex codes etc.

---

Step Five:
Now for the index file...

Copy all the code from the text box below and save it as index.php:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>//yourdomain.com :: The URL Shortening Site!</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>
<body>
<center>
</br>
</br>
<div class="header"><div class="inside">yourdomain.com</div></div>
<div class="main">
</br>
</br>
</br><?php
include("config.php");
$c = $_POST['url'];
$go = $_POST['go'];
if (!$go) {
echo "<form method='post' action=''>";
echo "HTTP://<input name='url' value='' />";
echo "<input name='go' value='Make Short URL' type='submit' />";
echo "</form>";
}
else if ($c) {
@mysql_connect ($server, $username, $password);
@mysql_select_db ($database);
$query = "SELECT id FROM url ORDER BY id DESC LIMIT 0,1";
$r = mysql_query($query);
while ($row = @mysql_fetch_array($r)) {
$lastid = $row[id];
}
$newid = $lastid + 1;
mysql_query ("INSERT INTO url VALUES ('','$c')");
mkdir("$newid");
$file = fopen("$newid/index.php", "w");
$c = "$c";
fwrite($file, "<? header("location:http://$c"); ?>");
fclose($file);
mysql_close();
echo "Thank you! Your new Short URL is http://www.yourdomain.com/$newid Go Back
 ";
} else {
echo "You did not specify your current URL
Go Back

";
}
?>
</br>
</br></div>
<div class="footer"><div class="inside">Short URL Scritp Copyright Oosband © 2006+ </div></div>
<div class="footer"><div class="inside">XHTML :: CSS </div></div>
</center>
</body>
</html>
Notes On Index.php:
Replace all the yourdomain.com with your actual domain...
You may have noticed that I added some copyright? If you don't want to keep that there, then please don't use the script. I spent a lot of time creating this, it's only one line too.
You may have also noticed that I have included two links...both are W3 Validator links, you don't have to keep them in, but they show that the code is XHTML and CSS valid...if you do keep them in remember to change the end of the link url.

---

Step Six:
All that is left now is to upload all the files to the public_html folder on your host, and then chmod the folder to 777.

---

Final Notes:
Don't remove your copyright line - This took me days to create.
Remember to chmod correctly.

Enjoy it!
  Reply With Quote

 Re: Short URL Script (PHP + HTML)
Old 11-15-2006, 11:00 AM   #2 (permalink)
Underground

Male gommle is offline
 
gommle's Avatar
 
Join Date: Sep 2006
Location: The o great land of Nooooooreeeway
Age: 20
Posts: 658
iTrader: 0 / 0%
gommle Is Recognizable
Rep Power: 7
Re: Short URL Script (PHP + HTML)

If I remember correctly, didn't you buy this tutorial from someone?
  Reply With Quote

 Re: Short URL Script (PHP + HTML)
Old 11-15-2006, 12:56 PM   #3 (permalink)
Alex
Guest

 
Posts: n/a
iTrader: / %
Rep Power:
Re: Short URL Script (PHP + HTML)

Nice tutorial :P
  Reply With Quote

 Re: Short URL Script (PHP + HTML)
Old 11-15-2006, 01:03 PM   #4 (permalink)
Underground

Male gommle is offline
 
gommle's Avatar
 
Join Date: Sep 2006
Location: The o great land of Nooooooreeeway
Age: 20
Posts: 658
iTrader: 0 / 0%
gommle Is Recognizable
Rep Power: 7
Re: Short URL Script (PHP + HTML)

One more thing: Why does it require a Linux server? I can't see any operating system specific functions or variables.
  Reply With Quote

 Re: Short URL Script (PHP + HTML)
Old 11-16-2006, 05:03 AM   #5 (permalink)
Oosband
Guest

 
Posts: n/a
iTrader: / %
Rep Power:
Re: Short URL Script (PHP + HTML)

gommle? What a pleasant supprise!
Well, I bought the script from someone, but I have full rights to it, I decided to make a tutorial.
And I was told it would only work on a linux server due to the fact public_html had to be chmodded.
  Reply With Quote

 Re: Short URL Script (PHP + HTML)
Old 11-16-2006, 06:26 AM   #6 (permalink)
Underground

Male gommle is offline
 
gommle's Avatar
 
Join Date: Sep 2006
Location: The o great land of Nooooooreeeway
Age: 20
Posts: 658
iTrader: 0 / 0%
gommle Is Recognizable
Rep Power: 7
Re: Short URL Script (PHP + HTML)

Rofl.

Well, it's correct that you can't chmod on a Windows server. But that doesn't mean it won't work, because all folders are similar to 777 by default.
  Reply With Quote

 Re: Short URL Script (PHP + HTML)
Old 11-17-2006, 05:27 PM   #7 (permalink)
unlimitedorb
Guest

 
Posts: n/a
iTrader: / %
Rep Power:
Re: Short URL Script (PHP + HTML)

Gommle is correct. There really is not reason for a Linux specific development environment in this case.
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
A Guide To Prefetching [HTML] Oosband Tutorials 0 10-18-2006 03:51 PM
Creating a PHP Script Timer Oosband Tutorials 0 10-18-2006 03:43 PM
Turn on HTML in SpamCity! Thy Gamer Website Suggestions 19 10-07-2006 11:07 AM
Who actually likes short shorts? second2none Chat 6 09-30-2006 03:10 PM

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 01:38 PM.


vBulletin skin developed by: eXtremepixels
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.16101909 seconds (100.00% PHP - 0% MySQL) with 20 queries