Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: trying to use #include but I can't get it to work!

  1. #11
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thankyou! Yes that works.
    Excuse my ignorance - but is the .php extension likely to affect anything else ie customers/users won't have any problems? I'll have to change all my pages to .php.

  2. #12
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There should be no problem renaming pages to .php, except that pages (if any) that were bookmarked as .html will no longer be there. You could leave a blank .html page behind for each though with a meta refresh tag on it that redirects to the PHP version. Or use .htaccess to redirect all calls to .html to their ,php versions.

    BTW, the 'top' page (the including page, like aptenia_test.php) needs to have the .php extension, but the included page - like linklist.php does not.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #13
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    BTW, the 'top' page (the including page, like aptenia_test.php) needs to have the .php extension, but the included page - like linklist.php does not.
    Absolutely correct-- any pages that include active <?php /*PHP*/ ?> must have the .php extension.
    On a technical note, I believe that adding the .php extension to the included page (the content) versus .htm will determine whether <?php ?> code blocks are parsed in the including page (the holder page) when that file's code is included. I'm not actually positive exactly how that works. But as a general rule: if you need PHP code from the second page to work in the first (in this case you do not), then use .php on both pages. If not, you can use .htm on the included page but always need .php for the including page. In general, though, I suggest using either .php or .htm consistently for the same content since that's easier to keep track of and allows for future use of any PHP code you might want, without the bookmarking problem John mentioned.

    The .php extension is irrelevant. PHP code, by fundamental design, generates HTML code which is in every way identical to ".htm" HTML code. Then the proper headers are sent by your server so that the browser knows it's an HTML-type file (even though it's .php). In fact, you can rename any .htm file you'd like to .php and you won't see any changes at all.*

    The only "difference" is the extension itself; all visitors will be able to view the page perfectly, but if you like the look and feel of the .htm ending, you won't get that with PHP. Here are three solutions, probably all overkill-- PHP is very common on the web-- look at the URL for the forum and you'll see .php there.
    1) Tell your server to parse .htm files as .php. This could potentially slow the server very slightly (probably not noticeably), and the result would be that any PHP code on any .htm page would be parsed like that. As a security warning, be sure that no one without admin privelages (eg, FTP access) can upload a .htm file. Just like a PHP file then, it could be used to hack your site. (This isn't a security issue unless you allow public uploading.) If you want to do that, you can search for it based on your server's configuration (or let us know the details). You would need configuration access to change the settings.
    2. Use .htaccess mod_rewrite (basically a way to "lie" to the visitor to use a fake URL to display a different page to mask all of the real .php files with .htm URLs. This puts very little burden on the server and it's not too hard to do, although for a beginner .htaccess is very difficult (in my experience). If you can find some code that's ready to go, though, it could be easy. Search around if you'd like.

    3. My personal suggestion is to NOT use any file URLs. Instead, create index.php OR index.htm files and use directory names.
    So your current site might have: mysite.com/mypage.htm. Instead, create an index.htm or index.php within the directory mysite.com/mypage/ and all of your pages will be very clean like that. It's a little bit of manual work (setting up all of those folders and storing all of the HTML files in different places), but the result is pretty URLs, and you also have a great added bonus that by referring to the URL of the directory rather than a file, it can refer to BOTH html and php, so bookmarks will always be valid for any type of file as the index page.

    I hope that answers your question. Let me know if anything was unclear.


    (*The only technical detail here is that because it's .php your server will check it for PHP code and try to run any PHP in it, so it might create a probably unnoticeable delay in serving the page-- a couple of milliseconds maybe. Unless you have an incredibly high traffic site, there's nothing to worry about. Either way, changing regular HTML files to PHP is certainly not required though.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #14
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by php.net/manual/en/function.include.php
    When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
    also, for using PHP without php file extensions:
    Code:
    #in your htaccess file
    # (ALWAYS keep your existing htaccess file - 
    #   add to it or comment lines out - NEVER overwrite it!)
    AddHandler application/x-httpd-php .php .htm .html
    # now all files with .php, .htm, OR .html will be parsed by php :)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •