Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something not right - session variables 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
My pages are working with PHP and MySql other than the following. Am working on localroot on pc.

The following extract shows the php code in the input box??
As it was a while ago, and on a remote site which worked, I cannot remember whether the session variable was first declared in other place, or if whats shown should work, or something amiss with my configuration of php?

<input name="QTY[1]" value="<?=$_SESSION['QTY'][1]?>" size="6" style="background-color: #FFFF00; font-weight:700" /><tr>
<?$_SESSION['PROD'][1]="DBC-6M"?>
<?$_SESSION['DURA'][1]="Digital Betacam 6 mins"?>

Many thanks
 
remember that you must start the session before trying to use it.
call session_start() at the top of the relevant script.

in past configurations you may have had session_autostart set to yes in php.ini. i recommend starting your sessions explicitly rather than automatically.
 
Many thanks, however session_start is started ealier on in the code, and session_autostart is not set, as I do it explicitly. I have also checked save sessions go somewhere.

All my pages that worked on a hosted site have the same problem, in that when the page opens, the input boxes are filled with <?=$_SESSION['QTY'][1]?> . Why are things so easy??, thanks
 
if i understand you correctly the form boxes contain the php code rather than the values that the php code should generate?

i am assuming that php is otherwise working for you (from your other posts).

your code appears to be formed ok which leaves us with just the short open tags issue to check next.
check your php.ini for the following setting:

Code:
short_open_tag = On

if it is on then php will understand just "<?". If not then it will only understand "<?php" and "<script>".

for these lines
Code:
<?$_SESSION['PROD'][1]="DBC-6M"?>
<?$_SESSION['DURA'][1]="Digital Betacam 6 mins"?>
i would add spaces after and before the open/close tags and also a semicolon at the end of the line.
Code:
<? $_SESSION['PROD'][1]="DBC-6M"; ?>
<? $_SESSION['DURA'][1]="Digital Betacam 6 mins"; ?>
 
Thanks jpadie, setting the short_open_tag = On has sorted out the input boxes, they now open blind on first viewing the page. Only problem now is if an entry is put in a textbox, you close the page, and then open it the previous input has gone, instead of being held in the session variable. All these pages/website works perfectly on a hosted site, so I am sure its down to my installation of php etc being a packaged install. Any ideas?, thanks again
 
the session cookie dies when you close the browser.

to change this you can set the php.ini directive session.cookie_lifetime to another number. 0 means that the cookie is deleted on close down. 31536000 is the right figure for a year's duration (in seconds).
 
Thanks again. Sorry, I will explain a bit more. There is a menu page and several product pages. If a user selects a product from the menu, a relevant page opens. Data is entered in textboxes and the user returns to the menu page to select another product. However he may return to the page he's just been in to add/change the data. Problem is its gone. So I am not closing the browser. Thanks again
 
can you post the code you use to display the session values? can you confirm that the data is not in the session store as well as not being displayed?
 
The Menu page selects product pages which are in the same format having input boxes.

CODE ON PRODUCT PAGE:
<input name="QTY[1]" value="<? =$_SESSION['QTY'][1]?>" size="6" style="background-color: #FFFF00; font-weight:700" /><tr>
<? $_SESSION['PROD'][1]="DBC-6M" ?>
<?$_SESSION['DURA'][1]="Digital Betacam 6 mins"?>

As well as session var QTY, 2 others are collected, ie PROD and DURA.

On selecting okay, it returns to the menu page.

I intercepted the menu page code and put:

<?php
session_start();
echo "Last Page = ".$_SESSION['PROD'][1]."<br>\n";
?>

However all that was displayed was - Last Page = , ie nothing. So the value of PROD got lost.

As I said, the website has been working on a hosted site, so I am convinced its in my configuration somewhere.

Thanks for your help


 
i'm not sure this will work
Code:
CODE ON PRODUCT PAGE:
<input name="QTY[1]" value="<? =$_SESSION['QTY'][1]?>" size="6" style="background-color: #FFFF00; font-weight:700" /><tr>
        <? $_SESSION['PROD'][1]="DBC-6M" ?>
        <?$_SESSION['DURA'][1]="Digital Betacam 6 mins"?>

could you replace with this and try again
Code:
CODE ON PRODUCT PAGE:
<input name="QTY[1]" value="<?= $_SESSION['QTY'][1]?>" size="6" style="background-color: #FFFF00; font-weight:700" /><tr>
<? 
$_SESSION['PROD'][1]="DBC-6M";
$_SESSION['DURA'][1]="Digital Betacam 6 mins";
?>
am assuming that the session was properly started earlier on the form page.
 
The code worked, but still no value carried over. Thanks
 
let's try and debug from scratch

can you create a new file and put the following code in it
Code:
<?
session_name("tek-tips-test");
session_set_cookie_params (24*60*60);
session_start();

echo "Currently stored in $_SESSION:<br/>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
echo "<hr/>";

if (isset($_POST['submit'])):
  $_SESSION['textbox'][]=trim($_POST['textbox']);
endif;

echo "After checking the incoming POST vars we have the following in $_SESSION:<br/>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
echo "<hr/>";

echo "<br/>";
$formvar = isset($_SESSION['textbox']) ? $_SESSION['textbox'][count($_SESSION['textbox'])-1] : "";
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="text" name="textbox" value="<?=$formvar?>"  />
<br/>
<input type="submit" name="submit" value="Test" />
</form>

and then post back the results from a couple of tests.
 
Thanks, the result. I see a rectangular box with
<?=$formvar?> inside it. I tried entering in it, and leaving as is. Pressing the test button gives me:

Forbidden
You don't have permission to access /< on this server.


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

Apache/2.0.58 (Win32) PHP/5.1.4 Server at 127.0.0.1 Port 80

Regards
 
either you have copied and pasted incorrectly or you have not turned on short tags.

here is a revised version that avoids short tags:

Code:
<?php
session_name("tek-tips-test");
session_set_cookie_params (24*60*60);
session_start();

echo "Currently stored in $_SESSION:<br/>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
echo "<hr/>";

if (isset($_POST['submit'])):
  $_SESSION['textbox'][]=trim($_POST['textbox']);
endif;

echo "After checking the incoming POST vars we have the following in $_SESSION:<br/>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
echo "<hr/>";

echo "<br/>";
$formvar = isset($_SESSION['textbox']) ? $_SESSION['textbox'][count($_SESSION['textbox'])-1] : "";
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="textbox" value="<? echo $formvar; ?>"  />
<br/>
<input type="submit" name="submit" value="Test" />
</form>
 
Thanks for all your effort, I replaced code with your new one, however with the same result, except the browser address changed to ['PHP_SELF'];?>

and the rectangular box contained <? echo $formvar; ?>

I had changed my short_open_tag to on earlier after you told me.

I have looked on the Wamps site, forum, but nobody reporting similar issues. Appreciate all your help to date. Regards
 
I have no idea what this could be caused by unless you have not saved the file as a php file or your php installation is fubar'd

here is a pure php file just in case that makes a difference.
Code:
<?php
session_name("tek-tips-test");
session_set_cookie_params (24*60*60);
session_start();

echo "Currently stored in $_SESSION:<br/>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
echo "<hr/>";

if (isset($_POST['submit'])):
  $_SESSION['textbox'][]=trim($_POST['textbox']);
endif;

echo "After checking the incoming POST vars we have the following in $_SESSION:<br/>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
echo "<hr/>";

echo "<br/>";
$formvar = isset($_SESSION['textbox']) ? $_SESSION['textbox'][count($_SESSION['textbox'])-1] : "";
echo <<<STR
<form method="post" action="{$_SERVER['PHP_SELF']}">
<input type="text" name="textbox" value="{$formvar}"  />
<br/>
<input type="submit" name="submit" value="Test" />
</form>

STR;
?>
 
you have not perfectly cut and pasted the code. i suspect you have changed something in the last two lines. perhaps adding a space after "STR;" (which will kill the script) or omitting the closing php tag.
 
I pasted the code in again, could not find reasons, tried changes around echo <<<STR. Then I repasted your previous code again, and got this :


Currently stored in Array:

Array
(
)

--------------------------------------------------------------------------------
After checking the incoming POST vars we have the following in Array:

Array
(
[textbox] => Array
(
[0] => TEST
)

)

It filled with my entry word Test. Hope this helps. Many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top