30 Replies Latest reply: Sep 13, 2011 1:33 PM by MurraySummers RSS

    PHP Includes

    cripaustin Community Member

      Hi!

       

      I'm just playing around with PHP includes but getting all confused with the links.

       

      Am I correct in saying that I have to use root relative links now? If there are images in the include files they need to be linked relative to the root?

      Also - the code on the page which includes in the include file needs to find the include file with a root relative reference?

      If we use document relatives then I will have to keep manually changing the code?

       

      if this is right - which I'm not sure that it is - then why is it that when I do this it isn't working? Is it because Dreamweaver isn't uploading the include files onto my testing server? Or is there another problem?

       

      Thanks!

       

      Chris

        • 1. Re: PHP Includes
          MurraySummers CommunityMVP

          Am I correct in saying that I have to use root relative links now? If there are images in the include files they need to be linked relative to the root?

           

          Yes.

           

          Also - the code on the page which includes in the include file needs to find the include file with a root relative reference?

           

          Doesn't matter, as long as the link is correct.

           

          Is it because Dreamweaver isn't uploading the include files onto my testing server?

           

          Check to see if it's there.

          • 2. Re: PHP Includes
            Rob Hecker2 CommunityMVP

            Also, if there is any question that the script cannot find the include file, change include() to require(). You will get an error if require can't find it, but include will not give an error.

            • 3. Re: PHP Includes
              Lon Winters Community Member

              Just to clarify, you don't need to change your site definition to root relative links. But any links within the include file itself should be root relative. Someone, please confirm or correct this!

              • 4. Re: PHP Includes
                MurraySummers CommunityMVP

                They only need to be root relative IF -

                 

                1.  The include files are kept within a folder of the site (mine are always in a folder called "_inc").

                2.  The site, itself, contains multiple folder levels which contain parent files for these includes.

                 

                If everything were at the root level of the site (i.e., no folder hierarchy) then document relative links would work throughout.

                • 5. Re: PHP Includes
                  cripaustin Community Member

                  Hi

                   

                  Thanks for replies everyone.

                   

                  So I've researched this further and from what I understand - the include PHP statement won't understand the root releative link reference.

                   

                  I've found some articles explaining how to solve this - but didn't really understand them.

                   

                  Anyone have a quick fix? How do I use PHP to find the same file (with a root relative ref) using the include statement? I don't want to have to change the include statement for every page.

                   

                   

                  Also: What do people think to using dreamweaver templates with PHP includes - so keep all the CSS link refs, PHP includes, etc in a template and then apply the template to each new page...this way you get the best of both worlds-? edit just a single include file to change parts of all the pages - but you also get that insurance and reliability of being able to update them all with the template if you really need to. Thoughts?

                   

                  Thanks

                   

                  Chris

                  • 6. Re: PHP Includes
                    MurraySummers CommunityMVP

                    So I've researched this further and from what I understand - the include PHP statement won't understand the root releative link reference.

                     

                    No PHP command will understand a root relative link, unless you amend it as follows -

                     

                    <?php include($_SERVER['DOCUMENT_ROOT'] . '/the_path_to_the_include_file.php'); ?>

                     

                    The reason you must do that is that Apache's root is not the same as the site's root, and when Apache sees the leading "/" it goes to its own root.

                     

                    Anyone have a quick fix? How do I use PHP to find the same file (with a root relative ref) using the include statement? I don't want to have to change the include statement for every page.

                     

                    You are still confused, I'm afraid.  The link to the include file is a document relative link, e.g.,

                     

                    <?php require_once("path_to_include_file.php"); ?>

                     

                    or it's an appropriately adjusted root relative link (as described above).

                     

                    The links IN the include file are root relative -

                     

                    <a href="/path_to_linked_file.php">Go to this linked file</a>

                     

                    Also: What do people think to using dreamweaver templates with PHP includes - so keep all the CSS link refs, PHP includes, etc in a template and then apply the template to each new page...this way you get the best of both worlds-?

                     

                    It's a technique I use on nearly every site I build.

                    • 7. Re: PHP Includes
                      Rob Hecker2 CommunityMVP
                      function(){return A.apply(null,[this].concat($A(arguments)))}

                      Also: What do people think to using dreamweaver templates with PHP includes - so keep all the CSS link refs, PHP includes, etc in a template and then apply the template to each new page...this way you get the best of both worlds-?


                      Murray responded by saying he uses this on nearly every site he builds. I will provide a different perspective.

                       

                      I have not touched Dreamweaver templates since I discovered includes four years ago.  I use includes and classes for frequently used elements, but my primary method is to build sites that only have one to six pages because the content is all stored in the database. I have a few websites that have only a single page. The admin systems of these sites may contain many pages, but the actual "website" only consists of one to six pages. This is similar to how frameworks like ZENDframework.

                       

                      Using this method, there is no difference between a template and the actual pages. PHP conditional statements can be used to handle many differences between content and presentation.

                      • 8. Re: PHP Includes
                        cripaustin Community Member

                        Thanks for replies.

                         

                        Interested about the 'few pages website' idea - this might be a silly question - but even if you use the database to change the content for each page - does this mean that the page exists or not? Can search engines find the page? Or do you make the page and then populate it with content using some sort of unique ID which links in with the content on your database?

                         

                        Sorry for this question - still getting into php, etc

                         

                        Thanks

                         

                        Chris

                        • 9. Re: PHP Includes
                          Rob Hecker2 CommunityMVP

                          Reasonable questions.

                           

                          Search engines have no problem with dynamically generated websites. It is possible to cache the pages (generate them ahead of time), but this is done for performance purposes only on sites that get tons of traffic.

                           

                          So when someone comes to the website, there actually are not pages there. It all gets built on the fly. The server can build a complex page with many calls to the database instantly. It's a beautiful thing.

                          • 10. Re: PHP Includes
                            Lon Winters Community Member

                            Murray already said it, but I'd like to reiterate, I guess I just feel like typing.

                             

                            The path TO the include file is document relative. Any links within the include file, like references to images, should be root relative. This means that you can include the include file in any page regardless of it's location, and any images or other links will follow along. The only problem that may present is when you call another include file from within the original include, then you need a more defined and consistent folder structure like Murray's set up.

                            • 11. Re: PHP Includes
                              osgood_ CommunityMVP

                              The way I work is to make all links in include files an 'absolute path' when I work locally and then do a 'find and replace' in Dreanmweaver substituting the local path with the actual domain name just before I upload the finished site.

                               

                              So this (locally)

                              <p><a href="http://localhost/site_name/index.php">index</a></p>
                              <p><a href="http://localhost/site_name/pages/eastbourne.php">Eastbourne</a></p>
                              <img src="http://localhost/site_name/images/eastbourne.jpg" width="200" height="170" alt="Eastbourne Seafront" />

                               

                              Becomes (remote)

                              <p><a href="http://www.domainName.com/index.php">index</a></p>
                              <p><a href="http://www.domainName.com/pages/eastbourne.php">Eastbourne</a></p>
                              <img src="http://www.domainName.com/images/eastbourne.jpg" width="200" height="170" alt="Eastbourne Seafront" />

                               

                               

                              Not sure if this is the best way but if I have 'includes' which need to be included into documents both at root level and in directories seems to work efficiently for me.

                              • 12. Re: PHP Includes
                                MurraySummers CommunityMVP

                                Using virtual hosts locally solves that issue.  But your method is a good workaround, too.

                                • 13. Re: PHP Includes
                                  osgood_ CommunityMVP

                                  Murray *ACP* wrote:

                                   

                                  Using virtual hosts locally solves that issue.  But your method is a good workaround, too.

                                   

                                  Never set virtual hosting up before is that complicated?

                                  • 14. Re: PHP Includes
                                    MurraySummers CommunityMVP

                                    Nope.  David Powers tells you how to do it in his book on CS5.  Pretty simple.

                                    • 15. Re: PHP Includes
                                      cripaustin Community Member

                                      Yo

                                       

                                      Thanks for help everyone. Php includes understood and sorted!

                                       

                                      I can build a site where all the pages are real from a template and where I change the ID variable for a page and have it build the page content from the database. This is fine but not so good for a big site (correct me if I'm wrong?)...Can anyone point me in the direction of a good web tutorial about creating the 'pageless' site? Where everything is made on the fly...

                                       

                                      So this sort of site doesn't affect search rankings? Because the spiders find the 'non-existent' pages and record them as if they do exist - so all is fine yeah?

                                       

                                      Thanks

                                       

                                      Chris

                                      • 16. Re: PHP Includes
                                        MurraySummers CommunityMVP

                                        The spiders are requesting the pages just the same way you as a visitor do.  As far as they/you know, the page is a complete, static page sitting on the server waiting for you to request it.  But the server knows better....

                                         

                                        I'm not aware of any tutorials for how to create a "mini-CMS" site, other than tutorials on how to use server scripting to retrieve data from a database and display it on a page.

                                        • 17. Re: PHP Includes
                                          Rob Hecker2 CommunityMVP

                                          Like many others here, I build sites that have hundreds of "pages" that change frequently and are maintained by non-technical staff. the dynamic method makes this possible. This is how virtually every large website is built, and most (but not all) are based on PHP/MySQL/Linux.

                                           

                                          A lot of people get a solid foundation in PHP from David Powers' books. Kevin Yank wrote a book called Database driven Websites using PHP and MySQL which is a good place to start for the kind of thing you are asking about. The Yank book is an easy breezy introduction, but it doesn't always use best practices. It gives you a practical overview with the gratification that you actually build something. I only have one of Powers' books, but I think I can generalize that he gives you a deeper comprehsion and uses better methods, but it takes more effort to work through his books. So I would say start with Yank, but with the understanding that you will need to continue on to one or more of the Powers books.

                                           

                                          To answer your question "does the dynamic method affect search rankings?" Different people will give widely different answers to that, but the short answer is that you are NOT penalized for using dynamic structures, which is the way all serious websites are built these days. Some people will say that you have to rewrite your urls to make them "SEO friendly" but Google has formally stated that this is almost never makes a difference and may actually hurt you.

                                          • 18. Re: PHP Includes
                                            MurraySummers CommunityMVP

                                            but Google has formally stated that this is almost never makes a difference and may actually hurt you.

                                             

                                            Do you have a citation for this, Rob?  My experience is not strong in this area, but logic tells me that a link like this -

                                             

                                            http://www.example.com/keyword/keyword.php

                                             

                                            will inherently rank better than a link like this -

                                             

                                            http://www.example.com/?id=5&whatever=foo

                                             

                                            And it seems that would be true even if you are doing a 301 redirect from the former to the latter.

                                            • 19. Re: PHP Includes
                                              cripaustin Community Member

                                              Thanks!

                                               

                                              Sorry I'm kind of using you guys as teachers - I do have Powers' book but I'm travelling around the world which gives me more time to sit on my macbook and make websites but also doesn't let me take any 1000 page books around with me

                                               

                                              So this might be my last question for a while because I'm sure I'm getting very annoying!

                                               

                                              So do the 'non-existing page' sites work like this:

                                               

                                              The URL holds some sort of ID. When a user whacks in that URL into their browser my .php ('template') page will load up and then find all my CSS formatting, etc and then it will see that the URL has an ID in. It will then set that ID as the $id variable which will then 'speak' with my database and find the record to which that $id concerns. Then the code will take all the data from that record and populate my page with nice content from the record.

                                               

                                              The spider also does this which means that my pages will be on the search engines - and as far as all people are concerned - a page exists for that thing - $id whatever...

                                               

                                              So can explain why all sites don't use this method? Why do people prefer to have 'real' pages (each perhaps made up by a dw template or ssi) which are individually given an ID to show data from the database - surely this is more time consuming and harder to maintain?

                                               

                                              Or is there an 'inbetween' method? Is it possible to have 'real' pages generated automatically when new records are added to databases? - new pages which are built inline with a dw template or have the ssi included?

                                               

                                              Maybe some of these questions are stupid but I'm really just looking for a general direction

                                               

                                              Thanks!

                                               

                                              Chris

                                               

                                              P.S. Also just found this http://www.webconfs.com/dynamic-urls-vs-static-urls-article-3.php

                                               

                                              Thoughts?

                                              • 20. Re: PHP Includes
                                                MurraySummers CommunityMVP

                                                P.S. Also just found this http://www.webconfs.com/dynamic-urls-vs-static-urls-article-3.php

                                                 

                                                Thoughts?

                                                 

                                                That's exactly the discussion we have just been having.  Consider, for example, this page -

                                                 

                                                http://www.theloftbroker.com/property/greenwich

                                                 

                                                If you visit that link, you will find this URL in the browser's address field when the page loads -

                                                 

                                                http://www.theloftbroker.com/indiv-detail.php?id=1

                                                 

                                                What is happening is exactly what is described in the link you provided. 

                                                 

                                                If you would visit all of the properties on that site, you will see that *all* of them are being displayed by that single page (indiv-detail.php).  And that's how the no-page (or more accurately, single-page) site works....

                                                 

                                                Why don't all sites use this method?  It's just another level of complexity for the developer to surmount.  And some sites' content doesn't map well into a data table of fields.

                                                • 21. Re: PHP Includes
                                                  osgood_ CommunityMVP

                                                  Murray *ACP* wrote:

                                                   

                                                  P.S. Also just found this http://www.webconfs.com/dynamic-urls-vs-static-urls-article-3.php

                                                   

                                                  Thoughts?

                                                   

                                                  That's exactly the discussion we have just been having.  Consider, for example, this page -

                                                   

                                                  http://www.theloftbroker.com/property/greenwich

                                                   

                                                  If you visit that link, you will find this URL in the browser's address field when the page loads -

                                                   

                                                  http://www.theloftbroker.com/indiv-detail.php?id=1

                                                   


                                                   

                                                  You should also say that the page is basically a template. If you just add the id=2 on the end of the link you get to another 'identical' page but with different information set out in the same way.

                                                   

                                                  http://www.theloftbroker.com/indiv-detail.php?id=2

                                                  The pages of the site which do not conform to the template layout still have to be produced individually:

                                                   

                                                  http://www.theloftbroker.com/about.html

                                                   

                                                   

                                                  So to clarify. If you have a lot of pages which carry the same basic information for different products then a database is a more steamline workflow to cut down on the number of pages you have to maintain.

                                                  • 22. Re: PHP Includes
                                                    MurraySummers CommunityMVP

                                                    Bingo.  Of course, you still have to maintain the info in the database....

                                                    • 23. Re: PHP Includes
                                                      Rob Hecker2 CommunityMVP

                                                      OK, this conversation is heating up but I for one have to get busy with work today, so I may hav eto drop out, at least for a while.

                                                       

                                                      Marray, I am sure I bookmarked the source of the info regarding rewrites. It came from the blog of a top Google manager. When I find it I will create a new post and name it something like "Murray, rewrite SEO issue."

                                                       

                                                      I am not a SEO expert but have of course been involved in several SEO projects, collaborating with experts (both real and, uh, not so real). My overall conclusion is that.

                                                       

                                                      1. SEO is a moving target and what was true last year may be completely untrue this year. I think the rewrite issue falls into that category.

                                                       

                                                      2. Content is king. To win status with Google, create a site people will flock to for the information it provides.

                                                       

                                                      3. Second to content is organization that the bots can make sense of. For instance, use H1 tag for main headers, and use descriptive titles and subtitles, etc.

                                                       

                                                      4. Links are next in importance, but make sure they are REAL links, not links to SEO link farms.

                                                       

                                                      5. Trying to cheat by making Google think your site is something it is not will likely backfire.

                                                       

                                                      The above is a simplification, and there are many other aspects, but as the SEO fads have come and gone, the above always holds true.

                                                       

                                                      Regarding the other general discussion, of when to use dynamic methods. If you already know how to build them, then I think it makes sense to use them for even very tiny websites.It is always a requirement to use the dynamic approach if you are building a site for clients to maintain through a web interface. For small sites that you manage yourself, you can leave out many of the web-based administration niceties.

                                                      • 24. Re: PHP Includes
                                                        MurraySummers CommunityMVP

                                                        Thanks, Rob - I couldn't take issue with any of the points you listed....

                                                        • 25. Re: PHP Includes
                                                          Rob Hecker2 CommunityMVP

                                                          Murray, I just found that url to the static/dynamic question. Here it is:

                                                           

                                                           

                                                          http://googlewebmastercentral.blogspot.com/2008/09/dynamic-urls-vs-static-urls.html

                                                          • 26. Re: PHP Includes
                                                            Lon Winters Community Member

                                                            Isn't this basically the way Wordpress works? If you look at the source, very few pages contain actual content of any sort, it's all generated on the fly. What it does use however are "template" type pages for page types, some are part of the WP source and some are part of the theme.

                                                            • 27. Re: PHP Includes
                                                              Rob Hecker2 CommunityMVP

                                                              Yes, it is exactly how WordPress works. (And Drupal and Joomla)

                                                               

                                                              WordPress themes are significantly differnt from Dreamweaver templates. WP themes are integrated into the database and even, sometimes, to the business logic. Overall, though, it's very hard to implement your business logic into a WP site.

                                                              • 28. Re: PHP Includes
                                                                MurraySummers CommunityMVP

                                                                Thanks.  I note that the date on the article is 2008.  And I note some fairly critical responses.

                                                                 

                                                                Seems to me that my argument still applies, too.

                                                                • 29. Re: PHP Includes
                                                                  Rob Hecker2 CommunityMVP

                                                                  Well, I think that what the article makes clear is that you are not doomed if you are not using rewrites, that GOOGLE does not penalize dynamic urls, and and that there are various considerations. So yes, your point is valid, but the situation is more complex than some people treat it.

                                                                  • 30. Re: PHP Includes
                                                                    MurraySummers CommunityMVP

                                                                    Definitely - but I figure this way: if you are passing up an opportunity to LEGALLY sneak keywords into semantic markup, then you are missing at least a part of the boat.  This is a way to do that, e.g.,

                                                                     

                                                                    http://www.example.com/keyword1/keyword2

                                                                     

                                                                    redirects to

                                                                     

                                                                    http://www.example.com/foo.php?id=23&whatever=foo