Using real domain names - hakakou/optiperl GitHub Wiki
Many scripts require you tell them the domain name they are running on. For most cases when offline, http://127.0.0.1/
or http://localhost/
will work. However when you upload, this must change to the real domain name. There are two solutions to this problem:
- Use the version converter. Usually a script will read the domain name into a variable, that it will use for the rest of the program. Modify to something like this:
#@Local#?#@Server
#? $domain = 'http://www.mysite.com/';
$domain = 'http://localhost/'; #?
After the upload this would become:
#@Server#?#@Local
$domain = 'http://www.mysite.com/'; #?
#? $domain = 'http://localhost/';
- Modify the computers hosts file.
The hosts
file (without extension) is a text file located:
- Windows NT/2000/XP:
c:\winnt\system32\drivers\etc\hosts
- Windows 98:
c:\windows\hosts
If it does not exists you can create it, by adding just one line like the below.
It's format looks like this:
127.0.0.1 localhost
What this file does is map IP addresses to host names. Usually it contains only one entry, the above, that tells windows that http://localhost/
is the same as http://127.0.0.1/
So what we can do is add another line:
127.0.0.1 www.mysite.com
That's what is needed! Now when http://www.mysite.com/ is called, the server at http://127.0.0.1/ will be invoked. This can be optiperl's internal server, or any other external server you are using.
You can also change the Host from Options / Run or Project options so OptiPerl also can use the new domain in it's requests.
The first time you call this mapped domain, you might get a window "Connect to" dialog prompting to connect to the internet; Don't connect, just press "Cancel".
But this line must be removed, when you then connect to the internet or else the real www.mysite.com will never be called when you want to!
Note also that the mapped domain does not have to only be used for a http protocol. You can map any domain for that is used for any kind of port. For example, if you are testing a script that uses mySQL, and you connect to your server's mySQL using
mysql.mysite.com,
then you can map mysql.mysite.com
in the hosts file so that your local mySQL server can be used.
IMPORTANT: Because of the above, if your have mapped a url like www.mysite.com, but your FTP address is also www.mysite.com (and not something like ftp.mysite.com) then while the url is mapped, your will not be able to connect to your real FTP site. This will happen becase for your computer www.mysite.com will mean 127.0.0.1, which also means itself. It will try to connect to an unavailable FTP server.
Automating when testing web sites
You can create a set of tools in OptiPerl to add and remove this line and integrate with the active project.
Add in one of the project's options data fields, like data7 the value www.mysite.com
Create a user tool:
Name: Map local IP to project
Program: MapIP.pl
Parameters: %data7% "<path to hosts>"
To unmap afterwards, create the following user tool:
Name: UnMap local IP to project
Program: UnMapIP.pl
Parameters: %data7% "<path to hosts>"
MapIP.pl and UnMapIP.pl are included in OptiPerl's installation. The
is replaced when OptiPerl is installed depending on your windows version. They add and remove the line:
127.0.0.1 site
where "site" is the first argument sent to the script.