473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Session_Start() Doesn't Work

Hi

I'm new to PHP and I'm trying to create a Login Form. Once the user has
logged in then he shouldn't have to log in again. The trouble is I'm getting
a new session ID between every page and so it doesn't recognise the user.
I've used Session_Start() which I thought was meant to maintain the session
variables between pages but it doesn't do work.

Any ideas or FAQ's?

Thanks
Chris
Jul 17 '05 #1
19 11116
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@hercules.btinternet.com...
Hi

I'm new to PHP and I'm trying to create a Login Form. Once the user has
logged in then he shouldn't have to log in again. The trouble is I'm getting a new session ID between every page and so it doesn't recognise the user.
I've used Session_Start() which I thought was meant to maintain the session variables between pages but it doesn't do work.

Any ideas or FAQ's?

Thanks
Chris


Are you using session_start on every page? Are you certain your system is
properly configured to store the sessions? Are you registering your
variables with the session so they are available on the "next" page?

- Virgil
Jul 17 '05 #2
> Are you using session_start on every page? Are you certain your system is
properly configured to store the sessions?
I dunno how do I check?
Are you registering your
variables with the session so they are available on the "next" page?
I've got session_start() on each page if that's what you mean.
"Virgil Green" <vj*@obsydian.com> wrote in message
news:9N*****************@newssvr24.news.prodigy.co m... "Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@hercules.btinternet.com...
Hi

I'm new to PHP and I'm trying to create a Login Form. Once the user has
logged in then he shouldn't have to log in again. The trouble is I'm

getting
a new session ID between every page and so it doesn't recognise the user. I've used Session_Start() which I thought was meant to maintain the

session
variables between pages but it doesn't do work.

Any ideas or FAQ's?

Thanks
Chris


Are you using session_start on every page? Are you certain your system is
properly configured to store the sessions? Are you registering your
variables with the session so they are available on the "next" page?

- Virgil

Jul 17 '05 #3
With total disregard for any kind of safety measures "Chris Allen"
<c.*****@nospam.co.uk> leapt forth and uttered:
Are you using session_start on every page? Are you certain your
system is properly configured to store the sessions?


I dunno how do I check?
Are you registering your
variables with the session so they are available on the "next"
page?


I've got session_start() on each page if that's what you mean.


No, you need to explicity define the variables you want stored in
the session.

For example:

<?php

session_start();

$_SESSION['my_var'] = 'Some data';

?>

--
Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/
Jul 17 '05 #4
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Are you using session_start on every page? Are you certain your system is properly configured to store the sessions?


I dunno how do I check?


View the output of a phpinfo() function. Checll all the 'session.*'
properties. Does it match your system and the way you want sessions to be
processed? Also check the url_rewriter.tags property.
Are you registering your
variables with the session so they are available on the "next" page?


I've got session_start() on each page if that's what you mean.


No, as Phil said, you have to put the fields in the $_SESSION array or, if
you are using the older scheme, use the session_register() function for each
variable you want registered.

Only registered variables (either method) will be retained across page
calls.

- Virgil
Jul 17 '05 #5
Chris,

I ran across the same problem a couple of weeks ago. Some hosting
companies require special magic lines of code and incantations :) (I
don't know if you are using a hosting company but...) After much
searching on my hosting company's support site I found that not only
is it required to have the Session_Start() function at the top of
every page but additionally they required session_save_path() to be
explicity set as well at the top of every page. See below.

<?php
session_save_path("/some/path/with/permissions/for/your/account");
Session_Start();

$_SESSION['user']=$username;

?>

It worked for me.

There is also a function that will echo out the current
session_save_path if you're curious but I couldn't find it again.

buena suerte! my two cents.

Javier
ae**@deadspam.hotmail.com


On Mon, 19 Apr 2004 21:35:42 +0000 (UTC), "Chris Allen"
<c.*****@nospam.co.uk> wrote:
Hi

I'm new to PHP and I'm trying to create a Login Form. Once the user has
logged in then he shouldn't have to log in again. The trouble is I'm getting
a new session ID between every page and so it doesn't recognise the user.
I've used Session_Start() which I thought was meant to maintain the session
variables between pages but it doesn't do work.

Any ideas or FAQ's?

Thanks
Chris


Jul 17 '05 #6

Hello Again,

Thanks for you replies. Yes I am using $_SESSION['favcolour']='blue'. In
fact I created page1.php and page2.php from the PHP documentation on
session_start() and it doesn't work for me either. My favcolour=green and
animal=cat on page1 but are blank on page2. I've got low security and
privacy settings and I've tried IE, Mozilla & Opera.

Here's Page 1:

<?php
// page1.php

session_start();

echo 'Welcome to page #1<br><br>';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();

echo 'Session ID='.session_id().'<br />';
echo 'favcolor='.$_SESSION['favcolor'].'<br>';
echo 'animal='.$_SESSION['animal'];
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Here's page 2.

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br /><br>';

echo 'Session ID='.session_id().'<br />';
echo 'favcolour='.$_SESSION['favcolor'].'<br>'; // green
echo 'animal='.$_SESSION['animal'].'<br>'; // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>
Below are my session variables from phpinfo(). I don't know enough about
PHP yet to know if they're right or not.

Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_dividend 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off
"Virgil Green" <vj*@obsydian.com> wrote in message
news:Iy******************@newssvr22.news.prodigy.c om...
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Are you using session_start on every page? Are you certain your system is properly configured to store the sessions?
I dunno how do I check?


View the output of a phpinfo() function. Checll all the 'session.*'
properties. Does it match your system and the way you want sessions to be
processed? Also check the url_rewriter.tags property.
Are you registering your
variables with the session so they are available on the "next" page?


I've got session_start() on each page if that's what you mean.


No, as Phil said, you have to put the fields in the $_SESSION array or, if
you are using the older scheme, use the session_register() function for

each variable you want registered.

Only registered variables (either method) will be retained across page
calls.

- Virgil

Jul 17 '05 #7
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...

Hello Again,

Thanks for you replies. Yes I am using $_SESSION['favcolour']='blue'. In
fact I created page1.php and page2.php from the PHP documentation on
session_start() and it doesn't work for me either. My favcolour=green and
animal=cat on page1 but are blank on page2. I've got low security and
privacy settings and I've tried IE, Mozilla & Opera.

Here's Page 1:

<?php
// page1.php

session_start();

echo 'Welcome to page #1<br><br>';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();

echo 'Session ID='.session_id().'<br />';
echo 'favcolor='.$_SESSION['favcolor'].'<br>';
echo 'animal='.$_SESSION['animal'];
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Here's page 2.

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br /><br>';

echo 'Session ID='.session_id().'<br />';
echo 'favcolour='.$_SESSION['favcolor'].'<br>'; // green
echo 'animal='.$_SESSION['animal'].'<br>'; // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>
This code worked for me, including the rewriting of the URL to include the
session ID on the first pass (until the server knows if the cookie is
accepted).

I've noted the differences between your session settings and mine below.

What does your URL look like the first time you click on the first page2
link? Do that by starting the browser anew opening directly to the page1 and
clicking the first page 2 link. What is the URL at that point? Mine is

http://www.obsydian.com/testing/page...21d779a9b70bbf
f39ae56f

Is yours similar? You can test my version at
www.obsydian.com/testing/page1.php and you can view my phpinfo at
www.obsydian.com/test.php.
Below are my session variables from phpinfo(). I don't know enough about
PHP yet to know if they're right or not.

Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_dividend 1000 1000
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off


session.use_trans_sid On On

Jul 17 '05 #8
Hi

Ah a bit of progress!!

Hmm it works fine from your site. At first I didn't get anything at the top
of the browser when I clicked on the first page2.php then I made the changes
you suggested in the php.ini file and now I get :

http://sapphire/page2.php?PHPSESSID=...94c757977f9a6c

But my colour and animal are still blank on page2. Though now the session id
is being transfered.

Also I entered session.gc_divisor = 100 but it doesn't appear in phpinfo().
Here's my phpinfo() now:

Session Support enabled
Registered save handlers files user

Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_dividend 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid On On

"Virgil Green" <vj*@obsydian.com> wrote in message
news:sr***************@newssvr23.news.prodigy.com. ..
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...

Hello Again,

Thanks for you replies. Yes I am using $_SESSION['favcolour']='blue'. In
fact I created page1.php and page2.php from the PHP documentation on
session_start() and it doesn't work for me either. My favcolour=green and animal=cat on page1 but are blank on page2. I've got low security and
privacy settings and I've tried IE, Mozilla & Opera.

Here's Page 1:

<?php
// page1.php

session_start();

echo 'Welcome to page #1<br><br>';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();

echo 'Session ID='.session_id().'<br />';
echo 'favcolor='.$_SESSION['favcolor'].'<br>';
echo 'animal='.$_SESSION['animal'];
// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Here's page 2.

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br /><br>';

echo 'Session ID='.session_id().'<br />';
echo 'favcolour='.$_SESSION['favcolor'].'<br>'; // green
echo 'animal='.$_SESSION['animal'].'<br>'; // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>
This code worked for me, including the rewriting of the URL to include the
session ID on the first pass (until the server knows if the cookie is
accepted).

I've noted the differences between your session settings and mine below.

What does your URL look like the first time you click on the first page2
link? Do that by starting the browser anew opening directly to the page1

and clicking the first page 2 link. What is the URL at that point? Mine is

http://www.obsydian.com/testing/page...21d779a9b70bbf f39ae56f

Is yours similar? You can test my version at
www.obsydian.com/testing/page1.php and you can view my phpinfo at
www.obsydian.com/test.php.
Below are my session variables from phpinfo(). I don't know enough about PHP yet to know if they're right or not.

Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off


session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_dividend 1000 1000


session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off


session.use_trans_sid On On

Jul 17 '05 #9

"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@hercules.btinternet.com...
Hi

Ah a bit of progress!!

Hmm it works fine from your site. At first I didn't get anything at the top of the browser when I clicked on the first page2.php then I made the changes you suggested in the php.ini file and now I get :

http://sapphire/page2.php?PHPSESSID=...94c757977f9a6c

But my colour and animal are still blank on page2. Though now the session id is being transfered.
Make sure you have permissions to write t /tmp directory. Just a guess. I
don't know if you have all your error messages turned off or directed to a
log where they aren't being obvious.
Also I entered session.gc_divisor = 100 but it doesn't appear in

phpinfo().

I don't know why i have a gc_divisor and you have a gc_dividend setting.
Perhaps different versions of PHP. I would suggest *not* adding gc_divisor
if you didn't already have it.

- Virgil
Jul 17 '05 #10
Try this:

session_start();
$var = "test";
session_register("var");

Later, pages can access it as $HTTP_SESSION_VARS["var"].
~Eric

My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt
"Virgil Green" <vj*@obsydian.com> wrote in message
news:%i***************@newssvr23.news.prodigy.com. ..

"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@hercules.btinternet.com...
Hi

Ah a bit of progress!!

Hmm it works fine from your site. At first I didn't get anything at the top
of the browser when I clicked on the first page2.php then I made the

changes
you suggested in the php.ini file and now I get :

http://sapphire/page2.php?PHPSESSID=...94c757977f9a6c

But my colour and animal are still blank on page2. Though now the

session id
is being transfered.


Make sure you have permissions to write t /tmp directory. Just a guess. I
don't know if you have all your error messages turned off or directed to a
log where they aren't being obvious.
Also I entered session.gc_divisor = 100 but it doesn't appear in

phpinfo().

I don't know why i have a gc_divisor and you have a gc_dividend setting.
Perhaps different versions of PHP. I would suggest *not* adding gc_divisor
if you didn't already have it.

- Virgil

Jul 17 '05 #11
Hi thanks for your suggestion but that didn't work either. I couldn't access
$HTTP_SESSION_VARS["animal"] on the same page. Animal is blank.

Here's Page 1 now:

<?php
// page1.php

session_start();
$animal="cat";
session_register("animal");

echo 'Welcome To Page #1<br><br>';
$_SESSION['favcolor'] = 'green';

$_SESSION['time'] = time();

echo 'Session ID='.session_id().'<br />';
echo 'favcolor='.$_SESSION['favcolor'].'<br>';
echo 'animal='.$HTTP_SESSION_VARS["animal"];

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

"Eric Stein" <no@spam.com> wrote in message
news:c6**********@news.wplus.net...
Try this:

session_start();
$var = "test";
session_register("var");

Later, pages can access it as $HTTP_SESSION_VARS["var"].
~Eric

My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt
"Virgil Green" <vj*@obsydian.com> wrote in message
news:%i***************@newssvr23.news.prodigy.com. ..

"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@hercules.btinternet.com...
Hi

Ah a bit of progress!!

Hmm it works fine from your site. At first I didn't get anything at
the top
of the browser when I clicked on the first page2.php then I made the

changes
you suggested in the php.ini file and now I get :

http://sapphire/page2.php?PHPSESSID=...94c757977f9a6c

But my colour and animal are still blank on page2. Though now the session
id
is being transfered.


Make sure you have permissions to write t /tmp directory. Just a guess.

I don't know if you have all your error messages turned off or directed to a log where they aren't being obvious.
Also I entered session.gc_divisor = 100 but it doesn't appear in

phpinfo().

I don't know why i have a gc_divisor and you have a gc_dividend setting.
Perhaps different versions of PHP. I would suggest *not* adding gc_divisor if you didn't already have it.

- Virgil


Jul 17 '05 #12
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Hi thanks for your suggestion but that didn't work either. I couldn't access $HTTP_SESSION_VARS["animal"] on the same page. Animal is blank.

Here's Page 1 now:

<?php
// page1.php

session_start();
$animal="cat";
session_register("animal");

echo 'Welcome To Page #1<br><br>';
$_SESSION['favcolor'] = 'green';

$_SESSION['time'] = time();

echo 'Session ID='.session_id().'<br />';
echo 'favcolor='.$_SESSION['favcolor'].'<br>';
echo 'animal='.$HTTP_SESSION_VARS["animal"];

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>


Too weird. It must be in your configuration somewhere. The code I posted
back to you earlier works just fine on my server. Can you provide a link to
a full phpinfo() function?

- Virgil
Jul 17 '05 #13
Hi,

I'm afraid I can't as it's a local server not connected to the internet. I
think I'm going re-install it all.
Thanks very much for all your efforts. I've a learnt quite a bit because of
it.

Chris

"Virgil Green" <vj*@obsydian.com> wrote in message
news:h5*****************@newssvr23.news.prodigy.co m...
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Hi thanks for your suggestion but that didn't work either. I couldn't access
$HTTP_SESSION_VARS["animal"] on the same page. Animal is blank.

Here's Page 1 now:

<?php
// page1.php

session_start();
$animal="cat";
session_register("animal");

echo 'Welcome To Page #1<br><br>';
$_SESSION['favcolor'] = 'green';

$_SESSION['time'] = time();

echo 'Session ID='.session_id().'<br />';
echo 'favcolor='.$_SESSION['favcolor'].'<br>';
echo 'animal='.$HTTP_SESSION_VARS["animal"];

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>


Too weird. It must be in your configuration somewhere. The code I posted
back to you earlier works just fine on my server. Can you provide a link

to a full phpinfo() function?

- Virgil

Jul 17 '05 #14
Hey Guess What !!???

I removed PHP and Apache and upgraded to Apache 2 and upgraded to PHP 4.3.6
and the same f*%#***ng thing is still happening!!
It's got to be something to do with Windows.
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Hi,

I'm afraid I can't as it's a local server not connected to the internet. I
think I'm going re-install it all.
Thanks very much for all your efforts. I've a learnt quite a bit because of it.

Chris

"Virgil Green" <vj*@obsydian.com> wrote in message
news:h5*****************@newssvr23.news.prodigy.co m...
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Hi thanks for your suggestion but that didn't work either. I couldn't

access
$HTTP_SESSION_VARS["animal"] on the same page. Animal is blank.

Here's Page 1 now:

<?php
// page1.php

session_start();
$animal="cat";
session_register("animal");

echo 'Welcome To Page #1<br><br>';
$_SESSION['favcolor'] = 'green';

$_SESSION['time'] = time();

echo 'Session ID='.session_id().'<br />';
echo 'favcolor='.$_SESSION['favcolor'].'<br>';
echo 'animal='.$HTTP_SESSION_VARS["animal"];

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>


Too weird. It must be in your configuration somewhere. The code I posted
back to you earlier works just fine on my server. Can you provide a link

to
a full phpinfo() function?

- Virgil


Jul 17 '05 #15
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Hey Guess What !!???

I removed PHP and Apache and upgraded to Apache 2 and upgraded to PHP 4.3.6 and the same f*%#***ng thing is still happening!!
It's got to be something to do with Windows.


I do believe this is the first time you've mentioned you were using Windows.
Fix your session.save_path to point to a valid directory. you have it set to
/tmp.... a *nix setting. You need to point it to a valid directory.

- Virgil
Jul 17 '05 #16
BINGO!!!!

Thanks a million Virgil!!
"Virgil Green" <vj*@obsydian.com> wrote in message
news:z8******************@newssvr22.news.prodigy.c om...
"Chris Allen" <c.*****@nospam.co.uk> wrote in message
news:c6**********@titan.btinternet.com...
Hey Guess What !!???

I removed PHP and Apache and upgraded to Apache 2 and upgraded to PHP 4.3.6
and the same f*%#***ng thing is still happening!!
It's got to be something to do with Windows.


I do believe this is the first time you've mentioned you were using

Windows. Fix your session.save_path to point to a valid directory. you have it set to /tmp.... a *nix setting. You need to point it to a valid directory.

- Virgil

Jul 17 '05 #17
I got it to retain $_SESSION['sql'], however, I can retain
$_SESSION['hidden'] but I can't seem to re-retrieve it. Consider this
code:

PHP:
--------------------------------------------------------------------------------

class SearchView extends View {

function SearchView() {} // CONSTRUCTOR

function &retainFormElements(&$html) { // STATIC HTML METHOD
$global $_GET, $_POST;
if ((!is_array($_POST) || @sizeof(array_values($_POST)) == 0) &&
$_SESSION['hidden']) {
$html .= $_SESSION['hidden'];
} else {
$collection = ($_POST && is_array($_POST) &&
@sizeof(array_values($_POST)) > 0) ? $_POST : $_GET;
foreach ($collection as $key => $val) {
if (!is_array($val)) {
$hiddenHTMLRetainer .= "<input type=\"hidden\" name=\"$key\"
value=\"$val\">\n";
$html .= $hiddenHTMLRetainer;
} else {
foreach ($val as $innerKey => $indivVal) {
if ($indivVal) {
$hiddenHTMLRetainer .= "<input type=\"hidden\" name=\"${key}[";
if (!is_numeric($innerKey)) $hiddenHTMLRetainer .= $innerKey;
$hiddenHTMLRetainer .= "]\" value=\"$indivVal\">\n";
$html .= $hiddenHTMLRetainer;
}
}
}
}
$_SESSION['hidden'] = $hiddenHTMLRetainer;
}
return $html;
}

}

--------------------------------------------------------------------------------

This class method is called from the class-level scope in another
class method:
PHP:
--------------------------------------------------------------------------------

class SearchPerformer extends DBActionPerformer {

function SearchPerformer () {} // CONSTRUCTOR

function &displayResults() { // STATIC HTML METHOD
// ADD STUFF to $html
$html = SearchView::retainFormElements($html); // WORKS EXCEPT FOR
SESSION
// ADD MORE STUFF TO $html
return $html;
}

function doSearch() { // VOID METHOD
$sql = "select * from blah";
$_SESSION['sql'] = $sql;
}

}

--------------------------------------------------------------------------------

This is actually a dumbed-down version of my actual code except for
retainFormElements() method. I have a routine where I am doing search
querying and returning results. Requirement (this is WORK BTW) is to
allow for modification of the existing search query as well as sorting
the existing query.

The only way I could figure to do both successfully is to save the
$sql statement into one $_SESSION variable (when you sort so that you
have your pre-built SQL all over again without having to rebuild it
from scratch) and store the $_POST collection variables from the form
into another $_SESSION variable as an HTML <hidden> element string
block (so when you click the links to sort the search results, though
$_POST is gone your stuff is still there if you need to modify).

However, here's my problem: While $_SESSION['sql'] exists,
$_SESSION['hidden'] does not. Though both are set, $_SESSION['hidden']
vanishes while $_SESSION['sql'] persists.

I don't think it is as simple as a manual issue. I'm totally stuck on
this, still! I have only one main script, "index.php", with
"session_start()" at the very top of the script, using PHP 4.3.2 with
register globals off and all other default settings, server is Linux
Red Hat 7.3.

Thanx
Phil

Javier_ <ja****@yeahright.com> wrote in message news:<sc********************************@4ax.com>. ..
Chris,

I ran across the same problem a couple of weeks ago. Some hosting
companies require special magic lines of code and incantations :) (I
don't know if you are using a hosting company but...) After much
searching on my hosting company's support site I found that not only
is it required to have the Session_Start() function at the top of
every page but additionally they required session_save_path() to be
explicity set as well at the top of every page. See below.

<?php
session_save_path("/some/path/with/permissions/for/your/account");
Session_Start();

$_SESSION['user']=$username;

?>

It worked for me.

There is also a function that will echo out the current
session_save_path if you're curious but I couldn't find it again.

buena suerte! my two cents.

Javier
ae**@deadspam.hotmail.com


On Mon, 19 Apr 2004 21:35:42 +0000 (UTC), "Chris Allen"
<c.*****@nospam.co.uk> wrote:
Hi

I'm new to PHP and I'm trying to create a Login Form. Once the user has
logged in then he shouldn't have to log in again. The trouble is I'm getting
a new session ID between every page and so it doesn't recognise the user.
I've used Session_Start() which I thought was meant to maintain the session
variables between pages but it doesn't do work.

Any ideas or FAQ's?

Thanks
Chris

Jul 17 '05 #18
Very first lines of index.php:

[PHP]
session_start(); // USED FOR ANY STORED SESSION VARIABLES (ONLY HAS
TO BE SET HERE)
echo "PHPSESSID = $PHPSESSID<P>";
echo "SESSION the moment you first enter this script of $PHP_SELF is:
"; print_r(array_keys($_SESSION)); echo'<P>';
[/PHP]

This is what you see the moment you first go into index.php and have
not yet submitted a search query:

PHPSESSID = a964d0d6683757cafbe5576737ea5bf9

SESSION the moment you first enter this script of
/image_catalog/index.php is: Array ( [0] => search [1] => sql )
Ok, now you submitted a search query. You entered some form stuff and
submitted. You're now at the search results page (which is also
"index.php"); this is what you see:

PHPSESSID = a964d0d6683757cafbe5576737ea5bf9

SESSION the moment you first enter this script of
/image_catalog/index.php is: Array ( [0] => search [1] => sql )

GET: Array ( )

POST: Array ( [0] => image_name [1] => allAlbums [2] => album [3] =>
boolword [4] => keywords [5] => persons [6] => events [7] => image_alt
[8] => image_creation_start_month [9] => image_creation_start_day [10]
=> image_creation_start_year [11] => image_creation_end_month [12] =>
image_creation_end_day [13] => image_creation_end_year [14] =>
image_location_city [15] => image_location_state [16] =>
image_location_country [17] => sortBy [18] => search [19] =>
isFromSearch [20] => section )

SESSION: Array ( [0] => search [1] => sql )

PHPSESSID = a964d0d6683757cafbe5576737ea5bf9

SESSION after setting it is: Array ( [0] => search [1] => sql [2] =>
hidden )
(note: to save space and sensitive info I am not doing:
[PHP]print_r($_POST);[/PHP] but instead I'm using
[PHP]print_r(array_keys($_POST));[/PHP], and the same with $_GET)

$_SESSION['sql'] is set via an instance of SearchPerformer object that
runs the generateSearchSQL() method:

[PHP]
class SearchPerformer extends DBActionPerformer {

function SearchPerformer () { // CONSTRUCTOR
$this->doSearch(); // THIS METHOD RUNS generateSearchSQL()
}

function &displayResults() { // STATIC HTML STRING METHOD
$html = SearchView::retainFormElements($html); // SEE BELOW FOR
CODE DETAIL
// DO MORE STUFF TO $html
return $html;
}

function generateSearchSQL() { // SQL STRING METHOD
// BUILD $sql HERE WITH STUFF
$_SESSION['sql'] = $sql;
// DO MORE STUFF
return $sql;
}

}

$performer =& new SearchPerformer(); // CONSTRUCTOR
[/PHP]

So everything is fine so far; I have $_SESSION['sql'] and I even have
$_SESSION['hidden'] which is set via the displayResults() method in
the SearchPerformer object, it running this class method (class
SearchView is *NOT* instantiated here!!!):

[PHP]
class SearchView extends View {

function SearchView() {} // CONSTRUCTOR (you will be instantiating
SearchView objects elsewhere, just not in SearchPerformer's methods)

function &retainFormElements(&$html) { // STATIC HTML METHOD
print_r("<P>GET: "); print_r(array_keys($_GET));
print_r("<P>POST: "); print_r(array_keys($_POST));
print_r("<P>SESSION: "); print_r(array_keys($_SESSION));
print_r("<P>");
if ((!is_array($_POST) || @sizeof(array_values($_POST)) == 0) &&
$_SESSION['hidden']) {
$html .= $_SESSION['hidden'];
} else {
/*--------------------------------------------------------------------------------------------------------------------
Set $collection to either $_POST (if form post variables exist in
$_POST) or default to $_GET
to parse through either/or and produce HTML hidden elements in
either case
---------------------------------------------------------------------------------------------------------------------*/
$collection = ($_POST && is_array($_POST) &&
@sizeof(array_values($_POST)) > 0) ? $_POST : $_GET;
foreach ($collection as $key => $val) {
if (!is_array($val)) {
$hiddenHTMLRetainer .= "<input type=\"hidden\" name=\"$key\"
value=\"$val\">\n";
$html .= "<input type=\"hidden\" name=\"$key\"
value=\"$val\">\n"; // ADD FROM QS TO PASS BACK
} else {
foreach ($val as $innerKey => $indivVal) {
if ($indivVal) {
$hiddenHTMLRetainer .= "<input type=\"hidden\" name=\"${key}[";
$html .= "<input type=\"hidden\" name=\"${key}[";
if (!is_numeric($innerKey)) $html .= $innerKey; // NUMERICAL
KEYS DO NOT NEED TO BE PLACED INTO HTML ARRAY-FORMATTABLE FORM
ELEMENTS
if (!is_numeric($innerKey)) $hiddenHTMLRetainer .= $innerKey;
$html .= "]\" value=\"$indivVal\">\n"; // PUT IN INDIVIDUAL
ARRAY ELEMENTS
$hiddenHTMLRetainer .= "]\" value=\"$indivVal\">\n";
}
}
}
}
$_SESSION['hidden'] = $hiddenHTMLRetainer;
global $PHPSESSID;
print_r("PHPSESSID = "); print_r($PHPSESSID); print_r("<P>");
print_r('SESSION after setting it is: ');
print_r(array_keys($_SESSION));
}
return $html;
}

}
[/PHP]
Now, in the search results page I click a link to sort my results.
The link takes me back to, you guessed it, index.php (in fact, every
single portion of my application literally has only one address:
"index.php" and nowhere else). The moment I do that, this is what I
see:

PHPSESSID = a964d0d6683757cafbe5576737ea5bf9

SESSION the moment you first enter this script of
/mu-spin/image_catalog/index.php is: Array ( [0] => search [1] => sql
)

GET: Array ( [0] => section [1] => search [2] => isSort [3] => sortBy
)

POST: Array ( )

SESSION: Array ( [0] => search [1] => sql )

PHPSESSID = a964d0d6683757cafbe5576737ea5bf9

SESSION after setting it is: Array ( [0] => search [1] => sql [2] =>
hidden )
This is totally wrong!!! The $_SESSION['hidden'] variable is gone!!!
It is, in fact, erroneously RESET once again (with the wrong values),
whereas it was supposed to have been retained within $_SESSION
superglobal array, yet it is not.

I hope this makes it a bit more clear as to what is literally going
on.

Synopsis:

1) I go to index.php
2) I see $_SESSION
3) I submit form elements
4) I see $_SESSION
5) I add $_SESSION['sql'] in generateSearchSQL() method
6) I add $_SESSION['hidden'] in SearchView::retainFormElements()
method
7) I see $_SESSION with both elements
8) I click a link to go back to index.php
9) I see $_SESSION.. with just 'sql' and NOT 'hidden'
10) I tear my hair out, what little I have left!

Phil
Jul 17 '05 #19
Never mind, I got it!! It was a stray "global $_SESSION" in another
class method altogether!

Phil

so*****@erols.com (Phil Powell) wrote in message news:<1c**************************@posting.google. com>...
I got it to retain $_SESSION['sql'], however, I can retain
$_SESSION['hidden'] but I can't seem to re-retrieve it. Consider this
code:

PHP:
--------------------------------------------------------------------------------

class SearchView extends View {

function SearchView() {} // CONSTRUCTOR

function &retainFormElements(&$html) { // STATIC HTML METHOD
$global $_GET, $_POST;
if ((!is_array($_POST) || @sizeof(array_values($_POST)) == 0) &&
$_SESSION['hidden']) {
$html .= $_SESSION['hidden'];
} else {
$collection = ($_POST && is_array($_POST) &&
@sizeof(array_values($_POST)) > 0) ? $_POST : $_GET;
foreach ($collection as $key => $val) {
if (!is_array($val)) {
$hiddenHTMLRetainer .= "<input type=\"hidden\" name=\"$key\"
value=\"$val\">\n";
$html .= $hiddenHTMLRetainer;
} else {
foreach ($val as $innerKey => $indivVal) {
if ($indivVal) {
$hiddenHTMLRetainer .= "<input type=\"hidden\" name=\"${key}[";
if (!is_numeric($innerKey)) $hiddenHTMLRetainer .= $innerKey;
$hiddenHTMLRetainer .= "]\" value=\"$indivVal\">\n";
$html .= $hiddenHTMLRetainer;
}
}
}
}
$_SESSION['hidden'] = $hiddenHTMLRetainer;
}
return $html;
}

}

--------------------------------------------------------------------------------

This class method is called from the class-level scope in another
class method:
PHP:
--------------------------------------------------------------------------------

class SearchPerformer extends DBActionPerformer {

function SearchPerformer () {} // CONSTRUCTOR

function &displayResults() { // STATIC HTML METHOD
// ADD STUFF to $html
$html = SearchView::retainFormElements($html); // WORKS EXCEPT FOR
SESSION
// ADD MORE STUFF TO $html
return $html;
}

function doSearch() { // VOID METHOD
$sql = "select * from blah";
$_SESSION['sql'] = $sql;
}

}

--------------------------------------------------------------------------------

This is actually a dumbed-down version of my actual code except for
retainFormElements() method. I have a routine where I am doing search
querying and returning results. Requirement (this is WORK BTW) is to
allow for modification of the existing search query as well as sorting
the existing query.

The only way I could figure to do both successfully is to save the
$sql statement into one $_SESSION variable (when you sort so that you
have your pre-built SQL all over again without having to rebuild it
from scratch) and store the $_POST collection variables from the form
into another $_SESSION variable as an HTML <hidden> element string
block (so when you click the links to sort the search results, though
$_POST is gone your stuff is still there if you need to modify).

However, here's my problem: While $_SESSION['sql'] exists,
$_SESSION['hidden'] does not. Though both are set, $_SESSION['hidden']
vanishes while $_SESSION['sql'] persists.

I don't think it is as simple as a manual issue. I'm totally stuck on
this, still! I have only one main script, "index.php", with
"session_start()" at the very top of the script, using PHP 4.3.2 with
register globals off and all other default settings, server is Linux
Red Hat 7.3.

Thanx
Phil

Javier_ <ja****@yeahright.com> wrote in message news:<sc********************************@4ax.com>. ..
Chris,

I ran across the same problem a couple of weeks ago. Some hosting
companies require special magic lines of code and incantations :) (I
don't know if you are using a hosting company but...) After much
searching on my hosting company's support site I found that not only
is it required to have the Session_Start() function at the top of
every page but additionally they required session_save_path() to be
explicity set as well at the top of every page. See below.

<?php
session_save_path("/some/path/with/permissions/for/your/account");
Session_Start();

$_SESSION['user']=$username;

?>

It worked for me.

There is also a function that will echo out the current
session_save_path if you're curious but I couldn't find it again.

buena suerte! my two cents.

Javier
ae**@deadspam.hotmail.com


On Mon, 19 Apr 2004 21:35:42 +0000 (UTC), "Chris Allen"
<c.*****@nospam.co.uk> wrote:
Hi

I'm new to PHP and I'm trying to create a Login Form. Once the user has
logged in then he shouldn't have to log in again. The trouble is I'm getting
a new session ID between every page and so it doesn't recognise the user.
I've used Session_Start() which I thought was meant to maintain the session
variables between pages but it doesn't do work.

Any ideas or FAQ's?

Thanks
Chris

Jul 17 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: | last post by:
Hello, I hope someone will help me or I will have to dive from 11th floor. Why this --> test = @"Melanie "Jets" Riggs" doesnt work in asp? I need something like that and I dont know how to make it...
1
by: Station Media | last post by:
Hi, here my problem, i use stored procedure in ACCESS database(latest version), and i would like to use this procedure but it doesnt work, do you know why ? Procedure: PARAMETERS MyField Text...
3
by: Clouds | last post by:
Hi ! How do I add the dynamic event handler for a dropdownlist present in the itemtemplate of a datalist !! I am doing it in the itemdatabound event of the datalist but it doesnt work... I am...
0
by: Juna | last post by:
I have been working in vs2003, but now started to work in vs2005 but the problem, I have simple web application not website, which work i mean open in browser when we press F5 or run the...
1
Digital Don
by: Digital Don | last post by:
I am writing a program for Peg solitaire... To check for no repetition of previous states I use a Set for storage of Board states.. The pronblem is when I declare the set as type char i.e. set...
3
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it...
1
by: Dany13 | last post by:
hi all. i using some text box for input value and some localvarible for passing this data to dataset . give instance for correct row of dataset and data in data table . use one gird view for...
20
by: Hush | last post by:
Hi, The following code works fine in IE7 but FF returns with an error: Access denied to achieve the property Element.firstChild. In this line: nodes = xmlDoc.documentElement.childNodes; My...
9
by: AGP | last post by:
I've been scratching my head for weeks to understand why some code doesnt work for me. here is what i have: dim sVal as string = "13.2401516" dim x as double x = sVal debug.writeline ( x)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.