How to use 404 errors to make a URL shortener
Posted by: David
404 errors can be used for more than appologies
A project I created and implemented about 6 months ago was a simple URL shortener. I didn't go looking on how to make a URL shortener, I thought of this on my own, and while others might have done this before I was able to create and master this system.
How you can do it
- Make sure you know a little about PHP and MySQL, these two things are crucial.
- First, design a database that can hold a list of url's that people will enter, and also give each one an id. In the most simplistic setup you should only one table in your database with this information.
- Now, set up your site to allow a custom PHP page for the 404 error page.
- Write the PHP for your 404 page to get only the filename, or the part after the last forwardslash / on the url, for example it would be "touoik" if the URL someone entered was http://www.fake.foo/bar/touoik.
- Now after you can put the file name into a string variable, use this to search your database for the id of the url. Since this should be set up as a auto incrementing primary key you will be having numbers as the file name, and they should correlate with the id of a certain URL. Return a URL, or error with your query, then use some HTML to forward to the URL your query returned.
- The last bit is to create a form that will allow people to enter their URL to submit it to the database. The best thing to do with this form is to make sure that it delivers you a valid URL. Google some ways to find valid URLs with PHP.
It's that simple, but you can do so much more
Some simple ideas that I used in my URL shortener I made with 404.
- Limit an IP address to only making a certain number of short URLs a day. Do this by adding another column to your URL table in the database and editing your 404 query to check the count for a particular day.
- Another option I gave users was the ability to enable a preview of the URL someone was being forwarded to. To do this I added a bit of code when someone submitted a URL to check for the title of the webpage that the URL was for, then stored that in the URL database. Then on the 404 page I set up a check for the query to see if it was enabled for previews, if so I would put a delay in the HTML forward and display the page URL and give the person viewing the 404 the option to go back.
- Click tracking was another feature that I had enabled. For this I created another database table with the short URL primary ID as the foreign key and set up columns for the click ID as the primary key and the date clicked, and IP from as the unique attributes for the click. This was a little more complex but I added another section onto the site that allowed someone to input the short URL, or file name and they could see the history of the short URL click.
- Private and secure forwarding was provided by setting up the 404 page to check to see if passwords were enabled for the short URL. If so, I would have the 404 site show a form to submit the password. When submitted it would check for the password and URL ID. If both matched, you would be forwarded. I also set up the click history section to allow someone to make their URL history private.
- Limited redundant URLs to save space in the database. I used the submit form to check the database to see if a public short version of this URL already existed and if so I just gave that person the same short URL that was already in the database without creating another entry.
- Created lots more entries by using a higher base counting system and converted the URL ID's into these higher base numbers to provide shorter short URLs. For example, 10 would be "A" and 11 would be "B", which is similar to other base systems, but I used a base system that extended all the way to using all digits and capital and lower case letters.
Project ideas for your IT department
Posted by: David
What can my IT department do to improve?
I think a lot of issues that IT has is with the stakeholders that aren't directly involved with any projects. Basically, most issues IT has to deal with are typically from the IT users, either internal or external users of systems. You will want to create a system that can help you manage your own department better so that you can better handle the issues from the IT users.
How can I do that?
I have a couple of ideas that small to medium sized company's can overcome easily. Larger companies may even be able to try this project if they have the spare time to devote to it, unless they feel like they should make it a priority.
IDEAS:
- Inventory Management - How many printers do you have? Where are they? What are their IP addresses? What types of paper will they print? All of these are great questions that you will see very often. The idea of a project with inventory management would be to inventory your Printers, and all information you can about them and then place them on a map that your support staff can view and interact with through a web based module. This can be done with some very simple php, mysql and javascript.
- Expand on your inventory - Don't just stop with the printers. Try to keep your computer inventory up to date as well through the same method. This will take a lot more time and effort but if you can get a system up and running and keep it up to date then you will be glad you put them time into it.
- Priority ticketing - Find a way to create your own ticketing system, or integrate with another one to create a very well organized priority chart. This system could work similar to an outlook calendar and schedule times for certain events, and reschedule things with different priorities. Just have things automatically reschedule themselves if the tickets aren't closed on time.
Here's what I did
I did an inventory of all of my company's printers one week. There were about 50 laser printers that were shared on the network and located in several different buildings. To take the inventory I went to each printer and marked it's location on a map. I then took down information about the printer such as its hostname, FQDN, IP, number of trays, tray paper types and whether or not it had an envelope feeder.
With all of this information that I needed, I created a mysql database with appropriate relations to put each printer into a different building. This step was very simple, buildings in one table, and printers in the other. The printer table would have a foreign key of the building ids which that printer is in. I also went beyond that, but I'll get to that soon.
Once you created this simple database, input your information. Create your PHP scripts to show the information as you please about the printers. Now that you can see a list of your printers and their information, how do you know where they are located? Now you work with your printer locations you marked down.
Find a copy of blueprints, or a layout for your buildings and open it on your computer with some software (MS paint) that can help you find the coordinates on that image. Now add a field onto your database for the coordinates located on the image for your printer. Mark the printer location on the map with the printer's ID in the database. Once you have all the printers marked on the map and the coordinates in the database you can code your php to show the map you marked with locations. To top that, you can use the coordinates to create an imagemap that, with javascript can display a popup with some brief information about the printer and perhaps a link to that printers page for more detailed info.
Go even farther
Create a another table in your database for your printers page counts. This table with have the printer id as the foreign key.
Now, create a php script that will pull out the printed page counts for each printer. Most printers in businesses now have web based interaction that you can use to pull this information from automatically. Set up this script to run every day at midnight and you can keep track of just how much particular printers are being used, and if they need to be moved, gotten rid of, or have more purchased.
Building a “curved” table with php, mysql and html
Posted by: David
Why build a "curved" table
Many web designers like to build these "curved" tables in order to make their websites more aesthetically appealing. The tables aren't really curved obviously, but you can alter their contents to make them appear that way.
How to build a "curved" table
It is really simple. Your easiest solution if you are just going to make one out of HTML is to go to THIS LINK and follow that guide. If you are familiar with some web design and just haven't done this before I will save you some time and break it down in slow mode right now.
- Open up an image editing software.
- Create a 40x40 file (if you want a table with a 20px turn).
- Make sure the background is transparent.
- In the center of the file create a circle with the color of your table and extend it to the edge of your file working area.
- Crop the file into 4 separate files. You will want each corner of the circle saved into its own file.
- Save the corners of the circle, and then open up your HTML editor
- The first <td> tag in the first row of your table will need to have the top left circle image in it with the <img> tag. Since you are using a 20x20 image, set the <td width=20 height=20>.
- The next <td> tag in your first row of the table should be empty and only include a colspan option equalling the amount of columns you have in your table without the two edges. (N-2 = Number of columns - 2 curved columns).
- The last <td> tag in your first row of the table should have the top right circle image in it with the <img> tag.Since you are using a 20x20 image, set the <td width=20 height=20>.
- For all of the rows that will contain your information start with a <td> tag with a width of 20 and the bgcolor to match that of your curved column image. Also end the row with the exact same tag with the width of 20.
- Repeat steps 7-9 but with the respective images for the bottom.
- Publish it and make sure you tagged your table correctly.
How do I build a "curved" table with a loop using php and mysql?
Ok, now you have to pay attention. This is not quite nearly as easy.
Start by doing the usual and creating your database connection, and running your query and saving it to a variable, I use $r (standing for results).
Start by building the first row of your table.
echo '<table><tr bgcolor="#B6E6E7"><td width="20" height="20"><img src="topleft.png" /></td><td colspan="2"></td><img src="topright.png" /></td></tr>';
Now that you have the first row of your table you can begin your loop with your query. I have my loop set up to check if the row is odd or even in number that way to create a table with different alternating colors for each row.
$i = 1;
while ($row = mysqli_fetch_array($r)) { // LOOP THROUGH QUERY RESULTS
if ( $i&1 ) {// its an odd result
echo '<tr bgcolor="#B6E6E7"><td width="20"></td><td>'.$row[0].'</td><td>'.$row[1].'</td><td width="20"></td></tr>';
} else { //it's an even results
echo '<tr><td width="20" bgcolor="#B6E6E7"></td><td>'.$row[0].'</td><td>'.$row[1].'</td><td bgcolor="#B6E6E7" width="20"></td></tr>';}
$i ++;
} // END LOOP
Now if you noticed, the even rows had the <tr> tag with a bgcolor instead of the <td> tag. This is because I want those entire rows to match the color of the table corners. The odd rows only had the first and last <td> tags with the bgcolor selected so that it would appear our table was outlined.
The final thing you have to do is finish off your table by essentially doing the same thing you did in the table's first row.
echo '<tr bgcolor="#B6E6E7"><td width="20" height="20"><img src="bottomleft.png" /></td><td colspan="2"></td><img src="bottomright.png" /></td></tr></table>';
I hope at this point you can tell what is going on. The table is done. You can release your query results.
Now you can see something similar to
But with your data in it and the rows are alternating colors. Simple, yet appealing.


