I have an included file (header.php) that calls another included file (campaigns.php). Since header.php can be called from different files in different directories, it needs to call campaigns.php from the website root. I can't seem to figure out how to do this in PHP.

The code is:
include("/campaigns.php");
but that doesn't work.

How can I make a call to a file and tell PHP to start at the website root?

I am using PHP on a Windows box with IIS.

I am brand new to PHP.

Thanks.

    include("./campaigns.php"); 

    check it that works.. you forgot the "." which tells the script that campaigns.php is in the same folder

      That doesn't help.

      campaigns.php is not in the same folder, it will vary which is why I need a way to designate a mapping to the root of my website.

        include "../campaigns.php";
        

        Double dots 😉

          No, double dots tells the it to go up 1 level.

          Does anyone know if there is a way in PHP to designate or map to the root of the site.

          Like in HTML if my image folder is in the root, all I have to do is use /images. No matter where I am in the site it knows to go to the root to find the images folder.

          ColdFusion has mapping where you can tell it where you want / or anything else to map to.

          Can someone please tell me how to do this in PHP?

          Thanks.

            Hi,

            with e.g. apache you can use $_SERVER['DOCUMENT_ROOT'] to get the root dir.

            If your web server doesn't set that variable then use $SERVER['SCRIPT_FILENAME'], $SERVER['SCRIPT_NAME'] and str_replace to get the root dir.

            Thomas

              I am on windows with IIS

              echo $_SERVER['SCRIPT_FILENAME']; returns an error:

              Notice: Undefined index: SCRIPT_FILENAME

              I am brand new to PHP and am amazed that such a basic, necessary and simple concept like this is so difficult. Is all of PHP this difficult?

                Originally posted by cfGuyNew2php
                No, double dots tells the it to go up 1 level.

                No. Double dots work fine under both IIS and Apache.

                I have a folder structure of this...

                root/inc/misc/functions/functions.php

                and another like this...

                root/admin/baseclasses/init.php

                .. in my init.php (in the second line) is the line

                require_once '../inc/misc/functions/functions.php';

                That works just fine. Perhaps you should TRY it before ignoring my post ?

                  Okay, I tried that and like I said using ../ tells the file to go up one directory and throws an error for files where it doesn't need to go up a directory.

                  Thanks anyway.

                  I think I will stick with ColdFusion, it costs a bit more but it is far superior in pretty much every way.

                  Like the saying goes...you get what you pay for 🙂

                    Do you have $_SERVER['SCRIPT_NAME'] with IIS (I can't remember and don't have access to an IIS server now) ?

                    EDIT:
                    how does $_SERVER look like ?

                    print_r($_SERVER);

                    Thomas

                      $base = "C:/apache2/htdocs/personalsite3/version1/";
                      
                      function getroot($currentpath) {
                         $levels = substr_count($currentpath, "/");
                         $rootpath = "./";
                         for($i = 0; $i < $levels; $i++) {
                             $rootpath .= "../";
                         }
                         return $rootpath;
                      }
                      
                      echo getroot(str_replace($base,"",$_SERVER['PATH_TRANSLATED']));
                      

                      Obviously $base is the root folder of your website on your web server.

                      You're completely right about people getting what they pay for.. I assume you must code for free.

                        Onion - your assumption is correct, I do code for free. I am not a professional developer but have a couple of my own sites I made myself.

                        As for getting what you pay for, let's take a look at CF vs PHP...

                        Your PHP Code:
                        $base = "C:/apache2/htdocs/personalsite3/version1/";

                        function getroot($currentpath) {
                        $levels = substr_count($currentpath, "/");
                        $rootpath = "./";
                        for($i = 0; $i < $levels; $i++) {
                        $rootpath .= "../";
                        }
                        return $rootpath;
                        }

                        echo getroot(str_replace($base,"",$_SERVER['PATH_TRANSLATED']));

                        my CF Code:
                        /

                        Hmmmmmm, I think CF might be on to something. Are you just angry that CF technology allows non-developers like me to do what you do only easier?

                          I can use "/" in PHP on a linux or unix server. Not being able to use it in PHP on Windows is technically a Windows issue, not a PHP one. Its all to do with the filesystem. The guys who wrote CF basically wrote their own version of that little function I've given you, and hid it away in CF server. As PHP is primarily designed to run on Linux Rasmus and friends didn't include the same thing in PHP.

                          I'm not in the slightest bit angry with CF at being enabling non-programmers to write code. However.. I annoyed at you for assuming other languages should do the same. As for CF enabling you to do "what I do".. can CF do this: http://www.ooer.com/wordsearch/fruit/ .. you see, CF enables you to make basic dynamic sites. Thats lovely. But don't go assuming you can make anything in CF that I (or any of the other remotely competent PHPers here) can make in PHP. You can't.

                            Never really figured out a way to make money with a word search puzzle. If I did though, I am sure I could find one that I could plug in (like you did).

                            I'm far from the coding "expert" you are, but I am pretty confident I can accomplish anything PRACTICAL using CF that you can using PHP in a much more effecient manner.

                            Anyway, I'll keep you in mind in case I ever come across a project that needs a PHP developer.

                              Originally posted by cfGuyNew2php
                              I'm far from the coding "expert" you are, but I am pretty confident I can accomplish anything PRACTICAL using CF that you can using PHP in a much more effecient manner.

                              Sure you can. Thats why I'm the paid senior developer and you write things for free..

                              :queasy:

                                Write a Reply...