Got a question? Ask a developer in our Developer's Corner Forum
| findstr (Searching File Text in Windows) |
|
If you want to change some text on your site or find the code that's causing problems, as long as you have a clue (url variable, text from the page, etc), you can go searching for it in the code. To do this in Windows, there's a command line program called findstr that will search through files looking for a string or regular expression. As an example, when you log into a Joomla site with the wrong username and password, you get this error:
We can use the string "Username and password do not match or you do not have an account yet" as our clue to search for the code that is producing this message. First, start a Command Prompt by either going the Start -> Programs -> Accessories -> Command Prompt (names may be slightly different depending on your version of Windows) or by going to Start -> Run... and typing in cmd.exe. This will open a black window that allows you to type in commands to run:
First you will need to change directories to the directory for your website. If you are unsure where your website lives on your machine, check your Apache or IIS settings. On my machine, my Joomla installation is in the "\Program Files\Apache Software Foundation\Apache2.2\htdocs\Joomla_alm" directory, so I run the following command: cd "\Program Files\Apache Software Foundation\Apache2.2\htdocs\Joomla_alm" You can tell that your change directory command was successful if your command prompt changed. You can see in my example that the text before the cursor now shows my new path:
We can now use the findstr command to search for our message. I use the /s option to tell findstr to search in the current directory and all sub directories. I also use the /C option to tell findstr to use my string as a literal search string. If I did not include this, it would look for any of the words in our string rather than doing an exact match. To find out more about the options you can use for findstr (or most Windows Command Prompt programs), you can run the command with the option /?:
So now I run the following command: findstr /s /C:"Username and password do not match or you do not have an account yet" *
findstr found a match in the file "language\en-GB\en-GB.ini". This is a helpful step, but this isn't the code we are looking for. The language files are used to translate from one string to another to allow for any number of languages. The value on the left of the equal sign is the string that will be used in the code. The value on the right is the text Joomla will send back to the user. We can now look for the value on the left to find the code that is producing this message by doing another findstr: findstr /s /C:"E_LOGIN_AUTHENTICATE" *
This time, we found a few references to the variable name in the language files as well as where it is used in the code. We can now open up the "libraries\joomla\application\application.php" file and see where the message is being produced. Most text editors also have searching capabilities. Open the file in your favorite text editor (notepad, wordpad, vim, etc), and search for E_LOGIN_AUTHENTICATE:
Once you find it, you can move up the file to see what function it is being used in. In our case, it is the login function:
Now what?! Well, that depends on why you were looking in the first place. If you wanted to change the message that the user is sent, you could change it in the language file we found. If you don't think you should be getting this message, you can add code to the php file and start debugging (for more information, see the article on debugging PHP using print statements). Step #1 is to find where you are in the code. Proceed onto other articles to find out all the wonderful things you can do once you are there. |









