Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
need help with session variables
Old 10-28-2008, 07:41 PM need help with session variables
Average Talker

Posts: 25
Trades: 0
I have edited the below phrase repeatidly and cannot get it to work. What do I have wrong here?

Code:
if( isset($_SESSION['current_page']))
   {
   //print( "email_call_page contains $_SESSION['current_page'] <br>" );
   print( "if is true <br>" );
   {
else
   print( "session variable current_page is not set <br>" );
bkelly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-28-2008, 07:44 PM Re: need help with session variables
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
"cannot get it to work" is a fairly broad statement.

what does it do? or not do, as the case maybe.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-28-2008, 07:55 PM Re: need help with session variables
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Have you started sessions using session_start() function before the point of output? If you think the sessions are in action, you can use this line to print out the session vars to see what is going on.

echo '<pre>' . htmlentities(print_r($_SESSION, 1)) . '</pre>';
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 10-28-2008, 08:48 PM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
I was not explicity enough in my OP. In page named mailform.php I have an html form with fields for data to send me in an email. It starts with:

Code:
 
<?php
session_start();
$_SESSION['current_page'] = "http://www.bkelly.ws/text/phpmailform.php";
?>
The submit button opens page mailsend.php. That page contains the php code to send the email. While developing, I have the following print statements to show and verify my variables:

Code:
<?php
... 
print( "email_to contains      $email_to <br>" );
print( "email_subject contains $email_subject <br>" );
print( "email_body contains    $email_body <br>" );
print( "email_reply contains   $email_replay <br>" );
print( "header contains        $headers <br>" );
print( "additional contains    $additional <br>" );
 
if( isset($_SESSION['current_page']))
//   {
//   print( "email_call_page contains $_SESSION['current_page'] <br>" );
   print( "if is true <br>" );
//   {
else
   print( "session variable current_page is not set <br>" );
...
?>

As shown here, the prints are displayed as I expect them to. When the three commented lines are uncommented, the page is displayed as all white and no text.

Currently the else is taken when I expect the IF to be taken. So the IF is killing the page even though the code is not being executed. I think the variable is not being set right, but I will work that after getting this page to display as desired.
bkelly is offline
Reply With Quote
View Public Profile
 
Old 10-29-2008, 04:44 AM Re: need help with session variables
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
you have to have session_start at the top of every page you intend to use the stored values.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-29-2008, 09:52 AM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Thanks chris. It does not crash the page now. I was under the impression that using session_start() again would be starting a new or second session.

I continue to have an error in setting a session variable. In my form page this code is at the top to set a session variable showing the page that initiates sending the email.
Code:
 
<?php
session_start();
$_SESSION['current_page'] = "http://www.bkelly.ws/text/phpmailform.php";
?>
Clicking the submit button opens the page that sends an email. In that page I have:
Code:
 
<?php
session_start();
?>
at the top, followed later on by:
Code:
if( isset($_SESSION['current_page']))
   print( "current_page contains $_SESSION['current_page'] <br>" );
else
   print( "session variable current_page is not set <br>" );
This is within a PHP island. It always takes the else indicating that the session variable is not set. What do I need to do to get the session variable set.

I do suspect that there is a variable somewhere that I can access to determine the previous page, but I don't know what it is and have not found it in my readings.

Begin edit
There is something obvious I am missing that is now frustrating me. When first starting I named my pages phpmailform.htm and phpmailsend.htm. I discovered the .php name requirement and changed the htm to php. I had the two pages working a little while ago. Just now I dropped the redundant php prefix and how have pages mailform.php (to fill in a form for sending me an email from my web page) and mailsend.php, the action page to actually send the email when the submit button is selected. After the name change, the following code causes a problem:

Code:
if( isset($_SESSION['current_page']))
   print( "current_page contains $_SESSION['current_page'] <br>" );
else
   print( "session variable current_page is not set <br>" );
If I comment it out, the page displays as I expect. Removing the comments causes the page to display blank, no text, no anything.

There appears to be something wrong with this code phrase, and I don't have a clue.

Is there a validator for php pages? I have found a couple for HTML, but they don't like the PHP code and declare an error.

Last edited by bkelly; 10-29-2008 at 10:21 AM.. Reason: Problem regression, clarification
bkelly is offline
Reply With Quote
View Public Profile
 
Old 10-29-2008, 10:30 AM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Quote:
Originally Posted by mgraphic View Post
Have you started sessions using session_start() function before the point of output? If you think the sessions are in action, you can use this line to print out the session vars to see what is going on.

echo '<pre>' . htmlentities(print_r($_SESSION, 1)) . '</pre>';
I added that line to my sendmail page and it displays as:

Quote:
Array
(
)
And it is displayed like that, with array, (, and ) each on separate lines. What does this tell us?

Just to save time, the page with the form and the page to send the email both have session_start() at the top before all html code;

BEGIN EDIT
I looked up the code phrase and I believe it should print out all the session variables. As it prints nothing, I take it the session is not valid. At the risk of redundancy, my page mailform.php begins with:

PHP Code:
<?php
session_start
();
$_SESSION['current_page'] = "http://www.bkelly.ws/text/mailform.php";
?>
and right after the <body> I put:
<?php
echo '<pre>' htmlentities(print_r($_SESSION1)) . '</pre>';  
?>
It displays like this:
Code:
Array
(
    [current_page] => http://www.bkelly.ws/text/mailform.php
)
and the page to send the email, mailsend.php, begins with:
PHP Code:
<?php
session_start
();
?>
// and the first thing after <body> is:
<?php
echo '<pre>' htmlentities(print_r($_SESSION1)) . '</pre>';  
?>
It displays as shown earlier in this post, nothing within the parens.

The session seems to be ok in the first page, but not in the second page. What do I need to do here?

Last edited by bkelly; 10-29-2008 at 10:50 AM.. Reason: More information
bkelly is offline
Reply With Quote
View Public Profile
 
Old 10-30-2008, 09:38 PM Re: need help with session variables
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Check your browser's security and cookie settings to make sure your browser is allowing to accept session cookies. Typically in conditions of where a browser is not accepting session cookies, php appends the session id (PHPSID) on the url parameter links of the page. Although I think the php.ini sets this auto option.

You can have a php function that checks if the constant SID is empty (or just echo it to the output) to the url param of a link reference. The SID constant holds the value of the session_name=session_id pair if the session needs to be passed to the next page because a cookie was not found.

For example, your first page that holds you form html would look similar to this:

PHP Code:
<body>
  <form name="mailform" action="mailform2.php?<?php echo SID?>" method="post">
 
the rest of your form and html stuff...
This would make sure that the session name and id would be passed to the second page without php creating a new session and losing the previous session data.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 10-31-2008, 02:54 PM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Responding to mgraphic:

I am using MSIE and the cookie setting is at medium.

At the top of mailform.php I have:
PHP Code:
<?php
session_start
();
echo 
SID;
print( 
"session started <br>");
?>
When I open the page it displays this at the top:
Code:
session started 
//
and nothing else. I do not see the session ID.
At the top of page mailsend.php is this code:
PHP Code:
<?php
session_start
();
echo 
SID;
print( 
"session started <br>");
?>
// and a little further down
<?php
echo '<pre>' htmlentities(print_r($_SESSION1)) . '</pre>';  
?>
That displays this:
Code:
 
session started 
Array
(
)
So it seems that in both places I am not getting the session ID. Is this a problem I can/should fix or should I present this to the site host.

Last edited by bkelly; 10-31-2008 at 03:02 PM.. Reason: format example display
bkelly is offline
Reply With Quote
View Public Profile
 
Old 11-01-2008, 09:26 AM Re: need help with session variables
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 565
Name: surajit ray
Location: inside the heart of my friends
Trades: 0
it seems interesting now. I guess you are redirecting to mailsend.php from mail mailform.php. Try session_write_close() function before the redirection. Did you tried the get method mgraphic suggested? It will work fine i guess. try with session_id() instead of SID.
__________________
I am not smart, that's why i don't act smart


Please login or register to view this content. Registration is FREE
jito is offline
Reply With Quote
View Public Profile
 
Old 11-01-2008, 02:41 PM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Quote:
Originally Posted by jito View Post
it seems interesting now. I guess you are redirecting to mailsend.php from mail mailform.php. Try session_write_close() function before the redirection. Did you tried the get method mgraphic suggested? It will work fine i guess. try with session_id() instead of SID.
I opened my editor and prepared to add in the session_write_close() statement, but then realized I don't know where to put it. Page mailform.php has a form that the user will complete to send me an email. The form contains a subject, body, and reply field. The user initiates the mail send clicking on the submit button. That button is define such that when it is clicked, page mailsend.php is opened. Here is part of the form definition:

PHP Code:
<form name = "mailform"
     method= "post" 
     enctype="multi-part/form-data" 
     action= "http://www.bkelly.ws/test/mailsend.php" <?php echo SID?> >
To
bkelly is offline
Reply With Quote
View Public Profile
 
Old 11-01-2008, 02:47 PM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Quote:
Originally Posted by jito View Post
it seems interesting now. I guess you are redirecting to mailsend.php from mail mailform.php. Try session_write_close() function before the redirection. Did you tried the get method mgraphic suggested? It will work fine i guess. try with session_id() instead of SID.
I opened my editor and prepared to add in the session_write_close() statement, but then realized I don't know where to put it. Page mailform.php has a form that the user will complete to send me an email. The form contains a subject, body, and reply field. The user initiates the mail send clicking on the submit button. That button is define such that when it is clicked, page mailsend.php is opened. Here is part of the form definition:

PHP Code:
<form name = "mailform"
     method= "post" 
     enctype="multi-part/form-data" 
     action= "http://www.bkelly.ws/test/mailsend.php" <?php echo SID?> >
To my knowledge, the "action= ..." line opens the next page. I don't see the results of the PHP statement echo SID. I don't know if it gets executed then wiped out, or not executed at all. There does not seem to be an explicit call into which I can place another function call.

Never the less, I will poke that function in the mailform.php page in a few places and see what happens.

edit

I am not seeing any results from the session_write_close() statement, but I may be using it wrong. I do continue to see what appears to be a discrepancy in the two pages. From the top of mailform.php:
PHP Code:
<?php
session_start
();
echo 
SID
echo 
'<pre>' htmlentities(print_r($_SESSION1)) . '</pre>';  
print( 
"session started <br>");
?>
This is displayed on the screen:
Quote:
Array
(
)


session started
//
The emailsend.php page has the same beginning and displays:
Quote:
Array
(
)
session started

HOWEVER: I just noticed a difference. When I close out my browser and start over, the two pages display this:

Quote:
PHPSESSID=586608aeafbba2280cdcff3b9accaf56
Array()
session started
//
and this

Quote:
Array()session started

The session ID is in the first page, but not the second, but only when just starting a new session. So,..., I don't know what's going on here. I guess this boils down to: How do I go from one page to the next without losing all the session data?

Last edited by bkelly; 11-01-2008 at 03:14 PM..
bkelly is offline
Reply With Quote
View Public Profile
 
Old 11-02-2008, 04:56 PM Re: need help with session variables
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
The only thing I can think of is maybe the location to where the session data files are written to do not have the correct read/write permissions for php, so the sessions are not being written providing blank data.

You can use session_save_path() function to determine your save path manually and make sure the dir has the correct permissions.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 11-02-2008, 09:32 PM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
I am not sure whats going on or where I should go next. I tried the session path. Both the first and the second form have the same initial path. Its not accessible to me at my hosted site. I created a new directory PHPSESSIONS and when I start a new session, I find some files in there of zero length. The first file is now displayed with:
Quote:
PHPSESSID=ea36bd783236c1a4b727d4591430f2c2
Array
(
)
prev path = /var/php_sessions
session path is PHPSESSION
session started
//
It has a session id. The second file open displays as:
Quote:
Array
(
)
prev path = /var/php_sessions
session path is PHPSESSION
session started
It has no session ID and starts with the same value. The two sessions are not related. So the question seems to be: How do I open a second page and retain the session from the first page?

Thanks for taking the time to help me.
bkelly is offline
Reply With Quote
View Public Profile
 
Old 11-03-2008, 04:27 AM Re: need help with session variables
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 565
Name: surajit ray
Location: inside the heart of my friends
Trades: 0
Really soory for this late reply. Ok, lets try to check it again:

PHP Code:
session_start();
echo 
"<pre>";
print_r($_SESSION);
echo
"</pre>" 
just create a page with the above lines of code ... run it in bot FF and IE. Can you see any diofference?
__________________
I am not smart, that's why i don't act smart


Please login or register to view this content. Registration is FREE
jito is offline
Reply With Quote
View Public Profile
 
Old 11-03-2008, 03:37 PM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Hello jito,
I don't care how long it takes to respond, any advice is appreciated.
with IE 7.0, the page displays as:
Quote:
Array
(
)
I used copy/paste so that is it exactly. I closed IE all the way and started FireFox. It provides exactly the
same display. I did add the opening and closing php notices as follows:
PHP Code:
<?php
session_start
();
echo 
"<pre>";
print_r($_SESSION);
echo
"</pre>" 
?>
My site is hosted by fatcow.com. Are you aware of them and do you think it matters? I am using a hidden page to do my testing. (Hidden only in the sense that there is no link to it and aware than crawlers can find it but don't care.) You can look at this page yourself and my email test pages at www.bkelly.ws/test. There are a couple of links on that page.

What information does this give you? Something useable I hope.

Regardless, thanks for taking the time to respond.
bkelly is offline
Reply With Quote
View Public Profile
 
Old 11-04-2008, 01:33 AM Re: need help with session variables
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 565
Name: surajit ray
Location: inside the heart of my friends
Trades: 0
Ahh , i m a idiot ! Didn't set anything to session, then can i test! try adding this line,

PHP Code:
echo session_id(); 
__________________
I am not smart, that's why i don't act smart


Please login or register to view this content. Registration is FREE
jito is offline
Reply With Quote
View Public Profile
 
Old 11-04-2008, 06:10 PM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Hello jito,
Not an idiot, just human like all of us.

with IE-7
Code:
b312869b8e63afa06c8eb1917fc7022aArray
(
)
with firefox the display contains:
Code:
2261914cfd3081b67f8d1861aed8e6b9Array
(
)
The displays are the same.
Back to the two files of interest, both start with this code at the very top:
PHP Code:
<?php
$prev_path 
session_save_pathPHPSESSION );
session_start();
echo 
SID;
echo 
'<pre>' htmlentities(print_r($_SESSION1)) . '</pre>';  
print( 
"prev path = $prev_path <br> ");
$session_path session_save_path();
print( 
"session path is $session_path <br>");
print( 
"<br> session started <br>");
?>
Under Firefox, the first page opens with this text:
Code:
PHPSESSID=f11baaadcafbf27eb3abc859eeb733ae
Array
(
)
prev path = /var/php_sessions
session path is PHPSESSION
session started
//
And the second page starts like this:
Code:
PHPSESSID=f11baaadcafbf27eb3abc859eeb733ae
Array
(
)
prev path = /var/php_sessions
session path is PHPSESSION
session started
Now this is unexpected. For the first time, the session IDs are the same. I don't know what changed. But then I tried IE-7. I am not getting the session ID. Sometimes I get it on the first page, but maybe it seems only if I close out the explorer and start over. Maybe I need to put in a statement to explicitly close the session ID. I'll look for that and try some more edits.
bkelly is offline
Reply With Quote
View Public Profile
 
Old 11-06-2008, 02:30 AM Re: need help with session variables
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 565
Name: surajit ray
Location: inside the heart of my friends
Trades: 0
I expected that it will work on FF, this is the problem with cookie based session. IE just unable to write the session to cookie before a request. So try to force it with session_write_close(). Hope that will solve the problem. Use that function before you are moving to the second page.

For testing: try registering some values in session:
$_SESSION['foo'] = 'bar'; in first page and check what the print_r returns now.
__________________
I am not smart, that's why i don't act smart


Please login or register to view this content. Registration is FREE
jito is offline
Reply With Quote
View Public Profile
 
Old 11-06-2008, 09:25 AM Re: need help with session variables
Average Talker

Posts: 25
Trades: 0
Quote:
Originally Posted by jito View Post
I expected that it will work on FF, this is the problem with cookie based session. IE just unable to write the session to cookie before a request. So try to force it with session_write_close(). Hope that will solve the problem. Use that function before you are moving to the second page.

For testing: try registering some values in session:
$_SESSION['foo'] = 'bar'; in first page and check what the print_r returns now.
Hello jito,
There are two things I don't understand from your post.

If I call session_write_close(), then how does the next page get the information from the current page. We just closed the session. That tells me the data is now gone. I did look this up in the php manual, but I suspect there is something about closing a session I don't understand.

Regarding the statement:
Quote:
Use that function before you are moving to the second page.
The current page (emailform.php) is an HTML form. When the user clicks on Submit, the next page (emailsend.php) is called. It seems to me that I don't know when the user will click the Submit button, and once the clicked, I have no further control before the next page is opened. How can I ensure that the session_write_close() is called before progressing from the emailform page to the emailsend page.
bkelly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to need help with session variables

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.64078 seconds with 12 queries