php-general Digest 31 Mar 2011 14:38:03 -0000 Issue 7252

Topics (messages 312146 through 312156):

Sessions - More Info
        312146 by: Ethan Rosenberg
        312147 by: Ashley Sheridan

is there a static constructor?
        312148 by: D. Dante Lorenso
        312149 by: Ryan
        312151 by: Peter Lind
        312155 by: Darren Karstens

Re: Sessions - More Info - SOLVED
        312150 by: Ethan Rosenberg

session_start() may take 5 seconds :(
        312152 by: Tolas Anon
        312153 by: Negin Nickparsa
        312154 by: Tolas Anon

Re: Path question
        312156 by: tedd

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Dear List -

Thank you for your help in the past.  This an update on my session problems.

Here is a simple test program. It never increments the session counter; ie, does not detect that $_SESSION has been set.

<?php  session_start();  ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
<html>
<body>

<?php


if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
        </body>
</html>

I have no idea what is wrong.

I need to make my session variables work so that I can finish a project.

Help and advice, please.

Ethan Rosenberg

MySQL 5.1 PHP 5.3.3-6 Linux [Debian (sid)]


--- End Message ---
--- Begin Message ---
On Wed, 2011-03-30 at 19:20 -0400, Ethan Rosenberg wrote:

> Dear List -
> 
> Thank you for your help in the past.  This an update on my session problems.
> 
> Here is a simple test program.  It never increments the session 
> counter; ie, does not detect that $_SESSION has been set.
> 
> <?php  session_start();  ?>
> 
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <html>
> <body>
> 
> <?php
> 
> 
> if(isset($_SESSION['views']))
> $_SESSION['views']=$_SESSION['views']+1;
> else
> $_SESSION['views']=1;
> echo "Views=". $_SESSION['views'];
> ?>
>          </body>
> </html>
> 
> I have no idea what is wrong.
> 
> I need to make my session variables work so that I can finish a project.
> 
> Help and advice, please.
> 
> Ethan Rosenberg
> 
> MySQL 5.1  PHP 5.3.3-6  Linux [Debian (sid)] 
> 
> 
> 


That code works perfectly for me, only thing I would change is the

$_SESSION['views']=$_SESSION['views']+1;

line to

$_SESSION['views']++;

for readability. If you're using Firefox, grab the Firebug plugin, which
should show you the headers that are being sent to and from the server
to the browser. From that, you might get an idea why the sessions don't
seem to be working. Just to make sure, turn on display_errors in your
php.ini file and restart Apache. Some whitespace (space or new line, for
example) before that first <?php line could cause the headers to send
and the sessions headers to fail (headers already sent error) which
would give you the problems you're seeing now. Also, some editors have
issues with the BOM (byte order marker) which could cause white-space to
be perceived where there is none. If you are sure there isn't any, then
try saving the script with a different character encoding to test if it
is the BOM causing problems.

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
All,

I want to build a config file class that gets called statically. Is there such a thing as a static constructor? Example:

class Daz_Config {
  public static function load() {
    ...
  }
  public static function get($key) {
    self :: load();
    ...
  }
}

Daz_Config :: get('myvalue');

I want to call the load function when the class is used for the first time. If no code ever calls "Daz_Config :: get(...)" then I never want to invoke load() but if it does get called, I only want to call load() once before the class is used further.

Anyone know how to do this with calling load() at the top of all the other functions and writing a load() function that exits early if already loaded?

-- Dante

--- End Message ---
--- Begin Message --- There is such thing, but its not called static constructor, its called singleton pattern
class SingletonA
{
  public static instance;
  public SingletonA(){}
  public static function getInstancce()
  {
    if(self::instance != null)
    {
      return self::instanc;
    }
    return new SingletonA();
  }
}

On 3/31/2011 12:56 AM, D. Dante Lorenso wrote:
All,

I want to build a config file class that gets called statically. Is there such a thing as a static constructor? Example:

class Daz_Config {
  public static function load() {
    ...
  }
  public static function get($key) {
    self :: load();
    ...
  }
}

Daz_Config :: get('myvalue');

I want to call the load function when the class is used for the first time. If no code ever calls "Daz_Config :: get(...)" then I never want to invoke load() but if it does get called, I only want to call load() once before the class is used further.

Anyone know how to do this with calling load() at the top of all the other functions and writing a load() function that exits early if already loaded?

-- Dante



--- End Message ---
--- Begin Message ---
On 31 March 2011 06:56, D. Dante Lorenso <da...@lorenso.com> wrote:
> All,
>
> I want to build a config file class that gets called statically.  Is there
> such a thing as a static constructor?  Example:
>
> class Daz_Config {
>  public static function load() {
>    ...
>  }
>  public static function get($key) {
>    self :: load();
>    ...
>  }
> }
>
> Daz_Config :: get('myvalue');
>
> I want to call the load function when the class is used for the first time.
>  If no code ever calls "Daz_Config :: get(...)" then I never want to invoke
> load() but if it does get called, I only want to call load() once before the
> class is used further.
>
> Anyone know how to do this with calling load() at the top of all the other
> functions and writing a load() function that exits early if already loaded?
>

The concept doesn't really make sense - a class that never gets
instantiated never gets constructed, hence no static constructor (nor
a static destructor). You'll have to call your "constructor" function
at the top of the static methods you'll be using - just check inside
the constructor if it's been called before or not.

Regards
Peter


-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>

--- End Message ---
--- Begin Message ---
class Daz_Config {
   private static $instance;

   private function __construct(){
      $this->load();
   }

   private function load() {
      ...
   }

   public function get($key) {
      ...
   }

   public static getInstance(){
      if (!self::$instance) self::$instance = new Daz_Config();
      return self::$instance;
   }
}

Then you can do:
Daz_Config::getInstance()->get('myvalue');

--- End Message ---
--- Begin Message ---
At 07:28 PM 3/30/2011, Ashley Sheridan wrote:
On Wed, 2011-03-30 at 19:20 -0400, Ethan Rosenberg wrote:

> Dear List -
>
> Thank you for your help in the past. This an update on my session problems.
>
> Here is a simple test program.  It never increments the session
> counter; ie, does not detect that $_SESSION has been set.
>
> <?php  session_start();  ?>
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <html>
> <body>
>
> <?php
>
>
> if(isset($_SESSION['views']))
> $_SESSION['views']=$_SESSION['views']+1;
> else
> $_SESSION['views']=1;
> echo "Views=". $_SESSION['views'];
> ?>
>          </body>
> </html>
>
> I have no idea what is wrong.
>
> I need to make my session variables work so that I can finish a project.
>
> Help and advice, please.
>
> Ethan Rosenberg
>
> MySQL 5.1  PHP 5.3.3-6  Linux [Debian (sid)]
>
>
>


That code works perfectly for me, only thing I would change is the

$_SESSION['views']=$_SESSION['views']+1;

line to

$_SESSION['views']++;

for readability. If you're using Firefox, grab the Firebug plugin, which
should show you the headers that are being sent to and from the server
to the browser. From that, you might get an idea why the sessions don't
seem to be working. Just to make sure, turn on display_errors in your
php.ini file and restart Apache. Some whitespace (space or new line, for
example) before that first <?php line could cause the headers to send
and the sessions headers to fail (headers already sent error) which
would give you the problems you're seeing now. Also, some editors have
issues with the BOM (byte order marker) which could cause white-space to
be perceived where there is none. If you are sure there isn't any, then
try saving the script with a different character encoding to test if it
is the BOM causing problems.

--
Thanks,
Ash
http://www.ashleysheridan.co.uk

++++++++++
Ash -

Thanks.

What did it was to 1] explicitly declare the character set and 2] close and restart Apache.

Ethan


--- End Message ---
--- Begin Message ---
My web-app sometimes takes just over 5 seconds to execute a single
start_session() statement.
No other connections are open to the site when i hit the button that
makes this happen, the session is just 78kb on a local disk, and it's
consistent behavior.
So far, it only happens when i request certain page content via AJAX,
but I'd like to get rid of it asap.
Strangely, some other page content requested under the same conditions
via the same pipeline returns without the delay. It doesn't do
anything differently except the business code, and the session is
opened in app-wide generic code that is executed before the business
code is executed. The very timing with microtime() happens before the
business code executes.

I've already googled, but haven't found anything useful.

It's on a windows box (latest wampserver) and due to a ubuntu firefox
limitation i can't test on linux at the moment, nor run strace on it
:(

--- End Message ---
--- Begin Message ---
session1.php:
<?php
session_start();
$_SESSION['s1']='sth';
header("location: session2.php?".SID);
?>
session2.php:
<?php
session_start();
if(isset($_SESSION['s1']))
echo $_SESSION['s1'];
else
die("No session!");
?>

--- End Message ---
--- Begin Message ---
On Thu, Mar 31, 2011 at 8:10 AM, Negin Nickparsa <nickpa...@gmail.com> wrote:
> session1.php:
> <?php
> session_start();
> $_SESSION['s1']='sth';
> header("location: session2.php?".SID);
> ?>
> session2.php:
> <?php
> session_start();
> if(isset($_SESSION['s1']))
> echo $_SESSION['s1'];
> else
> die("No session!");
> ?>
>
>

ok, that executes with success and fast..

--- End Message ---
--- Begin Message ---
At 9:18 PM -0400 3/28/11, Jack wrote:
Hello All,



Is there a smarter way to do includes by setting up a path or something
where I don't have to include /home/domain.com/includes/include_file.php

Apparently my path is as shown above,  but I would prefer to just put in
/includes/include_file.php

Thanks!

Jack


That's just a different between absolute and relative paths.

If you want to provide local files to a local script, then use "includes/include_file.php" -- that simply means in the same directory as the calling script there is a directory named "include" and within that directory is a file named "include_file.php".

Try it.

Sometimes people make this out to be harder than it is.

Cheers,

tedd
--
-------
http://sperling.com/

--- End Message ---

Reply via email to