include, globals and classes all in one?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wEEdpEckEr

    include, globals and classes all in one?

    Hi,

    here's the deal:
    I have a config.php file in which I have set a few var's, to use in the
    whole site. E.g.:

    $db_host = "localhost" ;

    Then I also have a class, which is supposed to handle all database
    functions. In this class I try to recover the var's from my config file,
    but this just doesn't seem to work. I tried to put the following
    statements in the constructor as well as in a plain function, but the
    output of the var is always empty.

    include("config .php");
    global $db_host;
    echo "value of db_host: ".$db_host;

    Now, I have a slight suspicion that it might have to do with the fact
    that I'm using classes, but I don't know what's wrong. Normally I should
    be able to use the values from the config.php file, right?

    greetz
    <T!M> aka wEEdpEckEr
  • Garp

    #2
    Re: include, globals and classes all in one?


    "wEEdpEckEr " <mijnnickname@p andora.be> wrote in message
    news:p2Hbc.1556 $Fn2.344099985@ hestia.telenet-ops.be...[color=blue]
    > Hi,
    >
    > here's the deal:
    > I have a config.php file in which I have set a few var's, to use in the
    > whole site. E.g.:
    >
    > $db_host = "localhost" ;
    >
    > Then I also have a class, which is supposed to handle all database
    > functions. In this class I try to recover the var's from my config file,
    > but this just doesn't seem to work. I tried to put the following
    > statements in the constructor as well as in a plain function, but the
    > output of the var is always empty.
    >
    > include("config .php");
    > global $db_host;
    > echo "value of db_host: ".$db_host;
    >
    > Now, I have a slight suspicion that it might have to do with the fact
    > that I'm using classes, but I don't know what's wrong. Normally I should
    > be able to use the values from the config.php file, right?
    >
    > greetz
    > <T!M> aka wEEdpEckEr[/color]

    Doing that works just fine with a simple example for me. Try
    error_reporting (E_ALL) and make sure the config file gets found, etc.

    Garp


    Comment

    • Stephen Gordon

      #3
      Re: include, globals and classes all in one?

      Garp wrote:[color=blue]
      > "wEEdpEckEr " <mijnnickname@p andora.be> wrote in message
      > news:p2Hbc.1556 $Fn2.344099985@ hestia.telenet-ops.be...
      >[color=green]
      >>Hi,
      >>
      >>here's the deal:
      >>I have a config.php file in which I have set a few var's, to use in the
      >>whole site. E.g.:
      >>
      >> $db_host = "localhost" ;
      >>
      >>Then I also have a class, which is supposed to handle all database
      >>functions. In this class I try to recover the var's from my config file,
      >>but this just doesn't seem to work. I tried to put the following
      >>statements in the constructor as well as in a plain function, but the
      >>output of the var is always empty.
      >>
      >> include("config .php");
      >> global $db_host;
      >> echo "value of db_host: ".$db_host;
      >>
      >>Now, I have a slight suspicion that it might have to do with the fact
      >>that I'm using classes, but I don't know what's wrong. Normally I should
      >>be able to use the values from the config.php file, right?
      >>
      >>greetz
      >><T!M> aka wEEdpEckEr[/color]
      >
      >
      > Doing that works just fine with a simple example for me. Try
      > error_reporting (E_ALL) and make sure the config file gets found, etc.
      >
      > Garp
      >
      >[/color]

      I generally use th following to define globals, nfi if it is really a
      correct way to do it tho.

      define("glb_MY_ GLOBAL", "myglobalvalue" );

      Access is the same as you have by doing:

      global glb_MY_GLOBAL;
      echo glb_MY_GLOBAL;

      -Steve

      Comment

      • Phil Roberts

        #4
        Re: include, globals and classes all in one?

        With total disregard for any kind of safety measures Stephen
        Gordon <s4054252@stude nt.uq.edu.au> leapt forth and uttered:
        [color=blue]
        > define("glb_MY_ GLOBAL", "myglobalvalue" );
        >
        > Access is the same as you have by doing:
        >
        > global glb_MY_GLOBAL;
        > echo glb_MY_GLOBAL;[/color]

        You don't have to globalize constants. They are already accessible
        globally.



        --
        Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

        Comment

        • wEEdpEckEr

          #5
          Re: include, globals and classes all in one?

          Stephen Gordon <s4054252@stude nt.uq.edu.au> wrote in news:c4o1ca$38k $1
          @bunyip.cc.uq.e du.au:
          [color=blue][color=green]
          >> Doing that works just fine with a simple example for me. Try
          >> error_reporting (E_ALL) and make sure the config file gets found, etc.[/color][/color]

          Here it doesn't... but I must admit, it's in a class (db_administrat or)
          of wich instances are used in another class (visitor), and there ara
          quite a few include statements involved.
          [color=blue]
          > I generally use th following to define globals, nfi if it is really a
          > correct way to do it tho.
          >
          > define("glb_MY_ GLOBAL", "myglobalvalue" );[/color]

          Indeed, this seems to work better. Good thing that their values don't
          have to change. ;-)

          Thanx both for your replies.

          Maybe just another question: I used an instance of Visitor to store in a
          _SESSION var, so _before_ I call session_start() , I'm obliged to include
          the class-definition of Visitor. But when I do this, I get an error
          Warning: session_start() : Cannot send session cookie - headers already
          sent by (output started at XXX/dbadmin.class.p hp:80) in XXX/register.php
          on line 4

          Now, dbadmin.class.p hp is ofcourse the class definition wich is called
          upon by visitor.class.p hp, but No where in the file I can find anything
          that would have 'sent headers', no output at all is used in that class,
          only calls to the database. The include to visitor.class.p hp is on line
          3 and start_session() on line 4 in register.php. Beneath is included the
          code of dbadmin.class.p hp, anyone of you know why this file would have
          sent headers before any instances of it are used? I have commented the
          "or die" statements that maybe could cause any output, but that won't
          help.

          TIA
          Tim

          ***dbadmin.clas s.php***
          <?

          class DatabaseAdminis trator {

          function DatabaseAdminis trator() {
          require("config .php");
          }

          function open_db () {
          global $link;
          $link = mysql_connect(d b_host, db_login, db_passw); //or die ("Can't
          connect to host: db_host!\n");
          mysql_select_db (db_name); // or die ("Can't select database
          db_name: $!\n");
          }

          function close_db () {
          global $link;
          mysql_close($li nk);
          }

          function add_hit() {

          }

          function add_visit($v_ip , $v_provider, $v_country, $v_date,
          $v_starttime, $v_browser, $v_os, $v_referer) {

          }

          function get_country($ip _addr) {
          $this->open_db();
          $table_name = db_table_ipcoun t;
          $query = "SELECT COUNTRY_NAME FROM $table_name WHERE ip_from <=
          $ip_addr AND ip_to >= $ip_addr";
          $result = mysql_query($qu ery);
          $row = mysql_fetch_arr ay($result);
          $this->close_db();
          return $row["COUNTRY_NA ME"];
          }
          }


          ?>

          Comment

          • Garp

            #6
            Re: include, globals and classes all in one?


            "wEEdpEckEr " <mijnnickname@p andora.be> wrote in message
            news:dfSbc.259$ KI1.117347504@h ebe.telenet-ops.be...
            <snip>[color=blue]
            > (output started at XXX/dbadmin.class.p hp:80)[/color]
            <snip>

            What's on this line? If it's not something you expect output from, stick a @
            in front of it, but it's likely it's doing something, possibly a header
            output of its own.

            Garp


            Comment

            • wEEdpEckEr

              #7
              Re: include, globals and classes all in one?

              "Garp" <garp7@no7.blue yonder.co.uk> wrote in
              news:3wSbc.2403 $oE1.23690378@n ews-text.cableinet. net:
              [color=blue]
              >
              > "wEEdpEckEr " <mijnnickname@p andora.be> wrote in message
              > news:dfSbc.259$ KI1.117347504@h ebe.telenet-ops.be...
              > <snip>[color=green]
              >> (output started at XXX/dbadmin.class.p hp:80)[/color]
              > <snip>
              >
              > What's on this line? If it's not something you expect output from,
              > stick a @ in front of it, but it's likely it's doing something,
              > possibly a header output of its own.[/color]

              Well, I left out a commented part, but apart from that, everything is in
              the previous post. The line refers to the last line of my class, "}". So
              it's not doing anything at all. And as you can see (code in previous
              message), there's not any output at all. Weird, no?

              Comment

              • wEEdpEckEr

                #8
                Re: include, globals and classes all in one?

                "Garp" <garp7@no7.blue yonder.co.uk> schreef op zo, 04 apr 2004 11:41:51
                GMT in news:3wSbc.2403 $oE1.23690378@n ews-text.cableinet. net:
                [color=blue]
                ><snip>[color=green]
                >> (output started at XXX/dbadmin.class.p hp:80)[/color]
                ><snip>
                >
                > What's on this line? If it's not something you expect output from,
                > stick a @ in front of it, but it's likely it's doing something,
                > possibly a header output of its own.[/color]

                Ok, looks like I found it: when using an include, the included page,
                shouldn't have <?php and ?> tags. The opening php tag will be ignored,
                because php is already opened, but the closing one will be used
                therefore terminating the php-code, even if it isn't supposed to do so.
                Stupid, when you don't know this. I thought all code had to be captured
                between these php-tags.

                greetz
                <T!M> aka wEEdpEckEr

                Comment

                • Pedro Graca

                  #9
                  Re: include, globals and classes all in one?

                  wEEdpEckEr wrote:[color=blue]
                  > Ok, looks like I found it: when using an include, the included page,
                  > shouldn't have <?php and ?> tags.[/color]

                  It *must* have <?php and ?> tags if it is going to be interpreted
                  [color=blue]
                  > The opening php tag will be ignored, because php is already opened,[/color]

                  PHP "closes" right after the include() and before opening the included
                  file. If the included file has PHP inside it must be delimited by the
                  php tags.
                  [color=blue]
                  > but the closing one will be used
                  > therefore terminating the php-code, even if it isn't supposed to do so.
                  > Stupid, when you don't know this. I thought all code had to be captured
                  > between these php-tags.[/color]

                  It has to be. You're doing some other thing wrong.

                  --
                  USENET would be a better place if everybody read: : mail address :
                  http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
                  http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
                  http://www.expita.com/nomime.html : to 10K bytes :

                  Comment

                  • Garp

                    #10
                    Re: include, globals and classes all in one?


                    "Pedro Graca" <hexkid@hotpop. com> wrote in message
                    news:c51sip$2p6 0g1$4@ID-203069.news.uni-berlin.de...[color=blue]
                    > wEEdpEckEr wrote:[color=green]
                    > > Ok, looks like I found it: when using an include, the included page,
                    > > shouldn't have <?php and ?> tags.[/color]
                    >
                    > It *must* have <?php and ?> tags if it is going to be interpreted
                    >[color=green]
                    > > The opening php tag will be ignored, because php is already opened,[/color]
                    >
                    > PHP "closes" right after the include() and before opening the included
                    > file. If the included file has PHP inside it must be delimited by the
                    > php tags.
                    >[color=green]
                    > > but the closing one will be used
                    > > therefore terminating the php-code, even if it isn't supposed to do so.
                    > > Stupid, when you don't know this. I thought all code had to be captured
                    > > between these php-tags.[/color]
                    >
                    > It has to be. You're doing some other thing wrong.
                    >[/color]

                    He's right, wEEdpEckEr. All my code is built this way, something else is
                    amiss. Try to reproduce the problem in two smaller files, and post them here
                    if it still fails s so we can try ourselves?

                    Garp


                    Comment

                    • wEEdpEckEr

                      #11
                      Re: include, globals and classes all in one?

                      "Garp" <garp7@no7.blue yonder.co.uk> schreef op do, 08 apr 2004 09:37:01
                      GMT in news:139dc.5362 $554.51055685@n ews-text.cableinet. net:
                      [color=blue][color=green]
                      >> It has to be. You're doing some other thing wrong.
                      >>[/color]
                      >
                      > He's right, wEEdpEckEr. All my code is built this way, something else
                      > is amiss. Try to reproduce the problem in two smaller files, and post
                      > them here if it still fails s so we can try ourselves?
                      >[/color]

                      Hmmm, ok, so I left out the closing tags, and that seemed to work, now
                      I've put them back, and again it seems to work fine. So it must've been
                      something completely different, that I've changed and solved without
                      knowing... Weird. ;-)

                      Btw: while I'm buggin you guys: getting the screen resolution is not
                      possible in php I guess? I've been searching the web, and I only can
                      seem to find references to javascripts...

                      greetz

                      Comment

                      • Phil Roberts

                        #12
                        Re: include, globals and classes all in one?

                        With total disregard for any kind of safety measures wEEdpEckEr
                        <toch_lekker_ni e@geen_email.au b> leapt forth and uttered:
                        [color=blue]
                        > Btw: while I'm buggin you guys: getting the screen resolution is
                        > not possible in php I guess? I've been searching the web, and I
                        > only can seem to find references to javascripts...
                        >
                        > greetz
                        >[/color]

                        PHP = Server Side
                        Screen Res = Client side.

                        So no, it's not possible. Besides, screen resolution is a red herring
                        as it makes no allowance for /window/ size.

                        --
                        Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

                        Comment

                        • wEEdpEckEr

                          #13
                          Re: include, globals and classes all in one?

                          Phil Roberts <philrob@HOLYfl atnetSHIT.net> schreef op do, 08 apr 2004
                          23:48:33 GMT in news:Xns94C683B DCB0Ephilrobert s@216.196.97.13 2:
                          [color=blue]
                          > PHP = Server Side
                          > Screen Res = Client side.[/color]

                          Well, that's your explaination :p Browsername and OS are also client
                          side and those can be easily checken within php. ;-)
                          [color=blue]
                          > So no, it's not possible. Besides, screen resolution is a red herring
                          > as it makes no allowance for /window/ size.[/color]

                          No, but it 's handy to know, weather most of your visitors are still in
                          800*600 or have advanced to 1024*768. This is pure for stats, not to
                          change the outcome of the page.

                          greetz
                          <T!M> aka wEEdpEckEr

                          Comment

                          • Ian.H

                            #14
                            Re: include, globals and classes all in one?

                            On Fri, 09 Apr 2004 00:00:10 +0000, wEEdpEckEr wrote:
                            [color=blue][color=green]
                            >> PHP = Server Side
                            >> Screen Res = Client side.[/color]
                            >
                            > Well, that's your explaination :p Browsername and OS are also client
                            > side and those can be easily checken within php. ;-)[/color]


                            [ snip ]


                            Browser name is sent by the client to the server hence PHP can detect this
                            value. The OS is often derived from this very same value also.

                            Check the user agent string in your logs.. normally, they'll contain the
                            browser and the OS the browser is running on.. neither of which are
                            actually _detected_ client side =)



                            Regards,

                            Ian

                            --
                            Ian.H
                            digiServ Network
                            London, UK


                            Comment

                            • wEEdpEckEr

                              #15
                              Re: include, globals and classes all in one?

                              "Ian.H" <ian@WINDOZEdig iserv.net> schreef op vr, 09 apr 2004 00:17:02
                              GMT in news:pan.2004.0 4.09.00.18.54.2 81000@bubbleboy .digiserv.net:
                              [color=blue]
                              > Browser name is sent by the client to the server hence PHP can detect
                              > this value. The OS is often derived from this very same value also.
                              >
                              > Check the user agent string in your logs.. normally, they'll contain
                              > the browser and the OS the browser is running on.. neither of which
                              > are actually _detected_ client side =)[/color]

                              Yes, I know, I wrote the regex's myself, but it could be that this is also
                              being send by the browser. Just like the referer-string, or something. This
                              is also not "detected", so it could have been possible that the resolution
                              was available too. Helas, it isn't... Too bad, since I can't use javascript
                              in my model.

                              greetz
                              Tim

                              Comment

                              Working...