Click here to Skip to main content
15,897,519 members
Articles / Programming Languages / PHP
Tip/Trick

A shortcut for your website

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
6 May 2013CPOL2 min read 19.2K   266   2   1
Generating an internet shortcut for your website from the server side

Introduction 

Have you ever wondered that having to remember and type the URL of your webpage over and again might be frustrating for some users of your website? That was why the internet shortcut was created to help out.

The Internet Shortcut / URL file (*.URL) is a Windows file which can however be used on other OS or better stated by browsers on other OS other than Windows. This is used extensively in IE to store bookmarks and favorites but can also be used to create a double clickable file (shortcut) similar to a file shortcut rather pointing to an internet resource and recognized by all major browser. 

The file format 

The file format is similar to an INI file and has the .URL extension (note .url doesn’t seem to work for some people but .URL works in all cases) 

[InternetShortcut]
URL=http://www.example.com/index.php
Workingdirctory=c:\Windows\
ShowCommand=7
IconIndex=1
IconFile=c:\Windows\system\url.dll
.
.
. 

There are still some other field but the most important are the URL and ShowCommand fields:

  1. URL: This is self-explanatory. It is the url of the internet resource. Note this is not limited to the http protocol, it can be http, https, ftp or some other supported protocols 
  2. ShowCommand: This the state the browser should be when the URL is opened. 0 or not setting this means normal, 3 means maximized, while 7 means minimized. 
  3. IconFile: This is the location of the file containing the icon that should be displayed for this file. This can be a .ico, .dll, .exe, or any file which can contain an icon file. Note that this is not limited to files on the local machine of the user, it can be on your webserver e.g. IconFile=http://www.example.com/myicon.ico  
  4. IconIndex: the index of the icon to use in the file specified in IconFile.

Benefits for generating this on the server side

Being able to generate this on the fly on the server side and letting the user download it their computer will be of great benefit in that

  1. The user can save the file on their desktop like a file shortcut but pointing to your website.
  2. Your website is one click away from the user.
  3. The user don’t have to remember your URL to visit your site.
  4. The file can be shared with friends and is OS independent compared to the Mac OS / Safari .webloc counterpart.

Generating on file from the server

The code below shows our to set the URL field (which is the only necessary and most important field to set) from the server side, preparing the file and shipping it to the user for download.

PHP
<?php
    $protocol = "http://";
    if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443)
    {
        $protocol = "https://";
    }
    $shortText = "[InternetShortcut]\nURL={$protocol}" . $_SERVER['SERVER_NAME'];
 
    // We'll be outputting an internet shortcut file
    header('Content-type: application/internet-shortcut');
 
    // It will be called myShortCut.URL
    header('Content-Disposition: attachment; filename="myShortCut.URL"');
 
    //output URL file
    echo $shortText;
 
?> 

The MIME (content-type) could be any of wwwserver/redirection, application/internet-shortcut, application/x-url, message/external-body, text/url, text/x-url but application/internet-shortcut works fine.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Nigeria Nigeria
A graduate of Agricultural Engineering from Ladoke Akintola University of Technology, Ogbomoso but computer and web programming is his first love. You can meet him on Facebook Osofem Inc.

Comments and Discussions

 
QuestionExcellent explanation Pin
Bob Mahone29-Jul-13 19:28
Bob Mahone29-Jul-13 19:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.