PHP & MyQL: Novice to Ninja, chapter 3, links1 example not working

In reference to ‘PHP & MyQL: Novice to Ninja’ edition 5, Yank, chapter 3. The file from example link1, name.html, when run in Firefox, and then clicking on “Hi I’m Kevin” results in Firefox attempting to open the name.php file instead of it being executed as expected. In Chromium, name.php is downloaded instead of executed. I assume this is a problem of some sort in the Apache2 PHP setup but much web-searching has not yielded any help, at least none that this newbie could understand. All other code in the book has worked fine and HTML/PHP executes ok in all previous examples. Only this new example where the href link to name.php seems to fail to invoke the expected Apache response. I’m running with Apche/2.2.22(Ubuntu 12.10 - Linux Mint 14) I’m hoping someone has seen this and knows how to resolve. I’ve looked at things like /etc/apach2.conf, mods-available and mods-enabled/php5.conf, etc. but nothing seems different than expected. name.html and name.php are in /var/www. All previous .html and .php examples worked ok from this location.

It sounds like Apache doesn’t understand it needs to be using PHP to process that file, so I’d go over the instructions of setting up Apache with PHP again to see if something was missed. If nothing was missed, be sure you restarted Apache after telling it how to process PHP files.

localhost/testphp.php works ok, but not sure if this means Apache or some other process is handling the testphp file. I’d like to assume this means Apache has PHP support enabled, at least for explicitly referenced files, but I’m a raw recruit indeed and have no business assuming anything :slight_smile:

Reviewing the book, Appendix A, page 421, I see a “<FilesMatch \.php$> SetHandler application” edit to be applied to /usr/local/apache2/conf/httpd.conf. This is described as an important command to setup Apache to recognize PHP scripts, and perhaps this is the very means by which browsers send script files to Apache. But httpd.conf does not exist on my system! (sudo su find / -name “httpd.conf”). In fact, the entire directory structures for Apache 2.2 and PHP5 appear to be completely different from those described in the book.

Girding up my loins and pressing on, relying on Google, I was able to discover /etc/apache2/mods-enabled/php5.conf with a similar command. However I notice this in php5.conf :

<FilesMatch “.+\.phps$”>
SetHandler application/x-httpd-php-source
# Deny access to raw php sources by default
# To re-enable it’s recommended to enable access to the files
# only in specific virtual host or directory
Order Deny,Allow
Deny from all

So now I’m wondering if .php files with content bounded by <?php … ?> are considered “raw” by the Apache server? If so, this /etc/apache2/mods-enabled/php5.conf seems to be denying their execution. Could this be the reason the browser tries to download these files instead of successfully handing them to the Apache server?

However, since I’ve purchased the book because I wanted a step-by-step tutorial for newbies and am too inexperienced to be working out what seem to be advanced level system configuration modifications, I am also wondering if it wouldn’t be better to start from scratch with Apache 2.4. Linux Mint 14’s Software Manager installs Apache 2.2.22. Perhaps 2.4 vs 2.2 explains the many file and directory structure differences between what is in the book and what is on my system. That’s a big step to take and it would be phenomenally dissapointing if after manually replacing Apache 2.2 with 2.4 I were to find httpd.conf still missing and php5.conf still containing the same FilesMatch Deny for PHP scripts.

Maybe commenting out the “Oder Deny, Allow” and “Deny from all” commands in php5.conf would be worth trying before getting crazy with re-installations.

/etc/apache2/mods-enabled are links to /etc/apache/mods-available, so I commented out Order Deny, Allow, etc. in php5.conf then restarted Apache. No change, still getting “Opening name.php” from Firefox when I click on “Hi I’m Kevin!”

So much for that theory.

I’m not sure what your php5.conf looks like, but this is what mine has:

<IfModule mod_php5.c>
    <FilesMatch "\\.ph(p3?|tml)$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    # To re-enable php in user directories comment the following lines
    # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    <IfModule mod_userdir.c>
        <Directory /home/*/public_html>
            php_admin_value engine Off
        </Directory>
    </IfModule>
</IfModule>

Got it working.

An error in my transcription of the book’s name.html example on page 58. I used href=‘name.php…’ instead of href=“name.php…”. Somewhere along the way I came to think ’ and " were interchangeable, and for the most part it seems ’ is used for strings instead of ". But for href, " appear to be required. So, changing from ’ to " fixes the problem and it works as expected.

Part of what caused me to take so long to find this is that I created a /home/user/public_html directoy and extracted the spbooks-PHPMYSQL5-f625bb6.zip there to get name.html. When executing that name.html from public_html I get the same behavior from Firefox (even with the correct href=“name.php”). But I left my incorrect version (href=‘name.php’) in /var/www and since it failed in the same way I was slow to find my error.

I’d still like to get /home/user/public_html working some day so I don’t have to do everything as root in /var/www, but that’s a problem for another day.

Thank you cpradio for commenting and keeping me engaged, I wouldn’t have stuck with it if I thought no one was listening. On to the next example…

That’s because the href=“” is part of the html structure of the page. It’s a rule in html that html attributes use " instead of '.

Turns out public_html support isn’t all that hard :

No more problems at all now using /home/user/public_html/name.html and name.php from the book’s examples.

Thanks, aaarrrggh (that was fun to type), I’ll keep in mind I need to pay attention to HTML vs PHP syntax. I’ll also be more disciplined in posting exactly what code I’m using, which would have allowed experts to catch this error much earlier.

One, hopefully last, thing I should mention concerning public_html is that /etc/apache2/mods-available/php5.conf has to have the <IfModule mod_userdir.c> section commented out (see below) in order for the public_html directory to work with the name.php link. Without the php5.conf modification, nothing shows up in Firefox although localhost/~user/name.php?name=Kevin appears in the URL.

Here’s my php5.conf, I undid the FilesMatch comments I had made earlier since they don’t seem to have any impact.

<IfModule mod_php5.c>
<FilesMatch “.+\.ph(p[345]?|t|tml)$”>
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch “.+\.phps$”>
SetHandler application/x-httpd-php-source
# Deny access to raw php sources by default
# To re-enable it’s recommended to enable access to the files
# only in specific virtual host or directory
Order Deny,Allow
Deny from all
</FilesMatch>
# Deny access to files without filename (e.g. ‘.php’)
<FilesMatch “^\.ph(p[345]?|t|tml|ps)$”>
Order Deny,Allow
Deny from all
</FilesMatch>

# Running PHP scripts in user directories is disabled by default
# 
# To re-enable PHP in user directories comment the following lines
# (from &lt;IfModule ...&gt; to &lt;/IfModule&gt;.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# &lt;IfModule mod_userdir.c&gt;
#     &lt;Directory /home/*/public_html&gt;
#         php_admin_value engine Off
#     &lt;/Directory&gt;
# &lt;/IfModule&gt;

</IfModule>

I’m having a problem with this same example. I’m on a mac and set up my server with MAMP as outlined in the book. I was able to successfully complete the “today” example from chapter 1 but with this example when I type the URL into my browser (http://localhost/name.html) all I get is a page of code:
<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“utf-8”>
<title>Query String Link Example</title>
</head>
<body>
<p><a href=“name.php?name=Kevin”>Hi, I’m Kevin!</a></p>
</body>
</html>

I copied the code from https://github.com/spbooks/PHPMYSQL5...hapter3/links1 and pasted them into text edit files and saved them as name.html and name.php.html in the htdocs folder in the MAMP folder in Applications.

I signed up with github and installed their program so I could download the actual files for chapter 3 but still get the same result (with Safari and Firefox). I tried copying one of my own html files in htdocs and it works fine.

Got it working! I guess the server kept accessing the old html file even though I moved it to the trash. Once I emptied the trash folder it used the new file. I’m guessing the reason it didn’t work in the first place was because I used textedit to create the html file. I’ll use textwrangler for html and netbeans for php from now on.