Re: [PHP] How to pass connection as global variable ?



C.R.Vegelin wrote:
C.R.Vegelin wrote:
I have various PHP scripts that use the same database.
The startup script default.php sets the connection once for all the scripts.
This connection is set in $_SESSION to make it a global variable for all scripts.
When switching from page default to page faq, I get errors I can't explain. Any help is highly appreciated.
TIA, Cor
default.php
----------------
<?php
session_start();
require("menu.php");

<snip>

faq.php
-----------
<?php require("menu.php");

The first two lines give it away ;)

You're missing a session_start().


--
Postgresql & php tutorials
http://www.designmagick.com/


Thanks Chris,

I included session_start(); as first line in faq.php, but now I get other warnings /errors:
Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli
in line "$result = mysqli_query($link, $sql);"
Warning: mysqli_error() [function.mysqli-error]: Couldn't fetch mysqli
in line "if (!$result) { echo "Can't execute query ($sql): " . mysqli_error($link); exit; }

Actually - shoot me for not noticing this before.

You can't store a resource or connection in the session.

You have to recreate your connection in each page.

See http://php.net/session .

--
Postgresql & php tutorials
http://www.designmagick.com/


Hi Chris,

I recreated a new connection in faq.php and it's working now.
I had the impression that 1 connection could last during a user session,
but apparently a user session may need many connections.

Thanks again, Cor

Remember that the http protocol is stateless. Every request made is a new connection to the server. It's due to extra technologies like cookies (and in this case sessions) that some form of 'state' can be maintained. However, because there is no communication between client and server unless the client makes a request you can never actually determine the _current_ status of your session, only in retrospect you can. If a user makes a request, you could say the session is 'in progress', however, after sending that page you don't know if it's still 'in progress' or has ended until you recieve another request, thus telling you it WAS still in progress.

Now, as for resources and connections: PHP clears up everything when it finishes sending a response to a request. This does not happen for certain very specific items which were stored away (like in a database or in a session, which is usually just a file). For a connection to be 'open' it needs to be able to communicate with both its' sides (client: your database; and the 'server': PHP), if php 'shuts down' because it finished the request, it doesn't leave anything of itself behind, and as such the 'server' of that connection also disappears, thus creating a broken link which is automatically destroyed. Of course the php engine is smart and knows this would happen, so it 'cleanly' closes the connection before disappearing to reappear again (fresh) on a next request.

hope that helps you understand,
- Tul
.



Relevant Pages

  • Re: APPN Connection z/os - AHHC / LLC2.
    ... implied that you session stopped for a while and then continued. ... the ANNC link is a type 2.1 connection. ... I would suggest trying to limit the request unit size used over the type 2.1 ... MAXDATA operand when defining a PU statement for an adjacent link station ...
    (bit.listserv.ibm-main)
  • Re: Remote desktop deadlock on XP SP2
    ... the TS connection is frozen, even if I specify a wait timeout. ... TermSrv.dll creates a new session for the purpose of displaying the logon ... lives on a DPC routine for the network miniport ... the network packets are encapsulated and decapsulated in UDP. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: JES-APPL
    ... Then there has been a session initiation request - which can only happen over an available session path - this isn't any of your flaky IP, ... Unless the NJE application on the AS/400 has consistently been caught in the middle of being activated in some way - highly unlikely I would expect - this is not a timing problem. ... Since the connection is necessarily in place, there is no problem with the connection - just in case a modem is involved - which, given a "switched" definition is used, is not impossible, but unlikely. ...
    (bit.listserv.ibm-main)
  • Re: os users, sessions, and connections.
    ... From what I've read a session is a logged connection. ... do the following (paddr is the address of the process ... select sid, serial#, paddr, username from v$session ...
    (comp.databases.oracle.server)
  • [NEWS] Vulnerability in the TCP Protocol Allows RST Spoofing (Cisco Advisory)
    ... A vulnerability in the Transmission Control Protocol (TCP) specification ... the connection may get automatically ... Here is an example of a normal termination of a TCP session: ... Access control lists should also be deployed as close to the edge ...
    (Securiteam)