FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » How to avoid the use of session variables in this script
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
How to avoid the use of session variables in this script [message #180781] Mon, 18 March 2013 23:05 Go to next message
daveh is currently offline  daveh
Messages: 18
Registered: March 2013
Karma: 0
Junior Member
Hello,

I have the following code snippet:

public function register($name, $rule, $dat_file, $server =
DEFAULT_SERVER, $port = DEFAULT_PORT)
{
if(in_array($name, $this->m_names))
{
$this->m_rules[$name][] = $rule;
return $_SESSION[$name];
}

// $cmd = "\$$name = new GTCM_GNP(\$dat_file, \$server, \
$port);";
// eval($cmd);

$$name = new GTCM_GNP($dat_file, $server, $port);
if(!$$name)
{
return FALSE;
}
$this->m_cnt++;
$this->m_names[] = $name;
$this->m_rules[$name] = array($rule);
return $$name;

What exactly does "return $_SESSION[$name]" do? it looks like it just
returns the $name argument in the function list. Or does it save it as
a session variable also? Why not just do: "return $name;" ?? Is there
a better way to do this without using session variables or super
globals? $_SESSION was originally $GLOBALS changing it to $_SESSION
did not break it and it seemed like a better alternative but probably
not the best way. By the way this code was originally written for php
4.0.6 if that makes any difference.

Thanks,

Dave
Re: How to avoid the use of session variables in this script [message #180783 is a reply to message #180781] Mon, 18 March 2013 23:48 Go to previous message
Robert Heller is currently offline  Robert Heller
Messages: 60
Registered: December 2010
Karma: 0
Member
At Mon, 18 Mar 2013 16:05:12 -0700 (PDT) David Heller <daveh(at)allheller(dot)net> wrote:

>
>
> Hello,
>
> I have the following code snippet:
>
> public function register($name, $rule, $dat_file, $server =
> DEFAULT_SERVER, $port = DEFAULT_PORT)
> {
> if(in_array($name, $this->m_names))
> {
> $this->m_rules[$name][] = $rule;
> return $_SESSION[$name];
> }
>
> // $cmd = "\$$name = new GTCM_GNP(\$dat_file, \$server, \
> $port);";
> // eval($cmd);
>
> $$name = new GTCM_GNP($dat_file, $server, $port);
> if(!$$name)
> {
> return FALSE;
> }
> $this->m_cnt++;
> $this->m_names[] = $name;
> $this->m_rules[$name] = array($rule);
> return $$name;
>
> What exactly does "return $_SESSION[$name]" do? it looks like it just
> returns the $name argument in the function list. Or does it save it as
> a session variable also? Why not just do: "return $name;" ?? Is there
> a better way to do this without using session variables or super
> globals? $_SESSION was originally $GLOBALS changing it to $_SESSION
> did not break it and it seemed like a better alternative but probably
> not the best way. By the way this code was originally written for php
> 4.0.6 if that makes any difference.

The _SESSION super global refers to a standardized PHP cookie.
Somewhere in the init code there should be a call to session_start().
What is going on 'under the hood' is that a cookie named PHPSESSID (or
something like that) is created and initialized. Setting
$_SESSION[<mumble>] to some value (where <mumble> is some arbitary key)
is a way of saving values across pages. The contents of the _SESSION
array is used to initialize the cookie when the headers are sent. When
some future page is accessed by the web browser that received this
cookie, it passes the cookie in the headers and session_start()
retrieves the cookie and re-initializes the _SESSION array from the
contents of the cookie.

The book "Learning PHP, MySQL & JavaScript", by Robin Nixon, Copyright
2009, published by O'Reilly Media, Inc. ISBN 978-0-596-15713-5 covers
the use of _SESSION on pages 289-296.

>
> Thanks,
>
> Dave
>
>

--
Robert Heller -- 978-544-6933 / heller(at)deepsoft(dot)com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: randomly sorting files in php
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Dec 04 18:52:59 GMT 2024

Total time taken to generate the page: 0.02666 seconds