1

How do I do this? It's not much of a hassle but at times it would be quite convenient if I could just double click on the php/html file to test my php scrips on my localhost/XAMPP server instead of the the double-click directing to me the location on the hard disk where the php server is not being used.

To clarify, I meant being able to skip typing the "localhost/" part of the file address in the browser address bar every time I want to test a php script.

Simon Suh
  • 10,599
  • 25
  • 86
  • 110

7 Answers7

2

One other thing to explore is writing a simple file at your DOCROOT called a.

That file would just redirect you to mysites/practice.php.

Then what you'd type in the browser is just http://localhost/a

Another piece to make this even easier: change your Windows HOSTS file. Introduce a shortcut for localhost, say l.

Then the address becomes just http://l/a

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59
1

You can't really. I suppose you could hack some solution where the associated windows program that opens php files (usually a text editor), was a script that launched the location in the browser. But that'd probably take a good amount of work from you to setup.

profitphp
  • 8,104
  • 2
  • 28
  • 21
1

How about PHP Command line ?

Or consider to use IDE that provide real-time preview like eclipse

One of the related posts : So eclipse and xdebug walk into a bar, and then my apache server dies

Community
  • 1
  • 1
ajreal
  • 46,720
  • 11
  • 89
  • 119
1

Edit your hosts file and change localhost to "1" or as you like you will be able to access root directory files as 1/file.php.Do the following steps

My Computer > C: (or whatever drive Windows is on) > WINDOWS > system32 > drivers > etc > hosts

Note: If you aren't sure where My Computer is or you just want to use a keyboard shortcut to open it, you can hold down the Windows key on your keyboard while you press the ā€œEā€ key.

Right-click on the HOSTS file, and click Open. A window will appear asking what to open it with; find Notepad, select it, and click OK.

If you've never edited your HOSTS file before, this is what it should look like:

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

127.0.0.1 localhost

change localhost to 1

you will be able to access the file using http://1/file.php

rajmohan
  • 1,618
  • 1
  • 15
  • 36
  • 1
    -1. Do not change localhost to 1. This will screw up many windows programs that EXPECT localhost alias to work. Instead, you need to ADD a line that maps 127.0.0.1 to the new alias. – Alex Weinstein Dec 29 '10 at 20:43
1

Create a file called C:\OpenLocalhost.vbs:

If WScript.Arguments.Count <> 1 Then
  WScript.Quit
End If
Set objShell = CreateObject("WScript.Shell")
objShell.Exec("cmd /c ""start http://localhost/" & WScript.Arguments(0) & """")

Double click on a .php file and set to open with the above script. Run regedit and find the command inside HKCU\Software\Classes\Applications\OpenLocalhost.vbs\shell\open\command and fix the actual command parameters:

Name        Type     Data
(Default)   REG_SZ   "C:\Windows\System32\cscript.exe" "C:\OpenLocalhost.vbs" "%1"

Now double clicking on a.php will launch your default browser to say http://localhost/C:/a.php

You will probably want to add rewrite rules to XAMPP or update the VBScript to point to your preferred document root.

Steve-o
  • 12,678
  • 2
  • 41
  • 60
0

Why not just mark the address php file in the favorites list of your browser or put a link on the menu bar (Chrome and IE and Firefox) then you just need to click the link in your browser.

When I am working on a project I do this as a temporary favorites link and it makes it very easy. When I am done, I just delete the link.

Another option is to create an html file in your development folder that contains the web links to the php files you are working with. You double click the file from your file system and it brings up the page. You click the link in the displayed page and it access through the URL specified which is the URL for your php script you want to test through the web server.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
0

You could do it simply with some javascript also. Place this script in your htdocs root folder and link it to any php page inside it <script src="autoLocal.js"></script>

(autoLocal= function() {                        // self-execution

    var localhost = "http://localhost/",        // this will replace the XAMPP folder
        regex = /file:///C://xampp/htdocs//g;   // default XAMPP folder             

    this.current =()=> {                        // turns the current path into a string

        return window.location.toString();
    }                   

    autoLocal.prototype.change =(()=> {         // checks if it matches and modifies the current path

        if (this.current().match(regex)) {

            var modified = 
                localhost + this.current().substring(
                    this.current().lastIndexOf(

                        (()=> {                 // fetches the page folder inside htdocs

                            var folder = this.current().substring(
                                    this.current().lastIndexOf(
                                    '/htdocs/')
                                +1);

                            return folder.split('/')[1]; 
                        })()
                    )
                );

            window.location.href = modified;    // spits out the modified URL and refresh page
        }
    })();

})();

When implemented, it will find the path to your page, replace the XAMPP part with localhost and refreshes the page. Simply click a php file and the rest will work on its own.

EDIT
I remade this script in a more prototype-based way for flexibility.
Simply clone aLocal.js at GitHub

Thielicious
  • 4,122
  • 2
  • 25
  • 35