Absolute and Relative Paths: Why my PHP / HTML / Javascript is not working
Several times on this blog, I have mentioned that the most common errors I make, and most programmers*, are the simple things like typing or forgetting to close a bracket or tag. Many of those errors are now automatically fixed by the intellisense of various IDEs (Integrated Development Environment), like Webstorm, but they still pop up.
Well, as I have said before (after nearly five years, almost everything on this blog I have said before), I started writing this blog because I wanted to remember solutions to problems for when I had the same problem six months later and was in a different state using a different computer at some random hotel. This is one of those problems.
We are developing a game that uses mostly javascript but since it runs on the web there is also html involved and some PHP (not written by me) that is used for writing the data from the game to our database.
Everything was working until Friday when all of a sudden the input forms quit inputting. I was able to look at the files on the server and see that a few of them had been changed on Friday. Obvious suspect, no?
Unfortunately, I had updated my local machine using the other person’s code from the server, so I had the non-working files on my machine also.
Of course, I had back-ups on my local machine. So, I copied those files over the newer ones on my local machine. This should bring me back to the pre-update state, where everything was working, yes? No.
How could this be? Files on local machine worked. Copied over by files on server. Copied original files that worked back on local machine (yes, they really were the correct files) and they did not work. How can that be?
Well, I gave it away in the title of this post. One of the javascript files had the ABSOLUTE path to the PHP file, not the relative path. So, when I ran it on my local machine, it accessed the non-working, newer, file on the server.
A billion thank-yous to the wonderful tech support staff at pair.com , our web host, who helped me figure this out on a Saturday night. They could see that there was an attempt to connect to the database, and so we knew that the javascript was working and it must be something in the PHP file. At which point I said,
“I can see the error in the PHP file on the remote server but that would only be a problem if the javascript had that file name coded in to call instead of a relative path …”
and then a lightbulb went off and it was all fixed in a minute.
Take away message – if things aren’t working in any way that makes sense, perhaps you are looking at the wrong files.
* The Rocket Scientist says that I should not call myself a programmer because programmers are lower-level and it is only a slightly more impressive title than code monkey. I am ignoring him for now, but if anyone has suggestions for new titles, I am listening. My business cards say “President”, which last week led a child just old enough to read, but not old enough to follow politics obviously, to ask me if I was the president of the country. He was very disappointed when I said no.