473,782 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use SESSION variable?

Hi,

If ever a newbie wants to know how much he has to learn yet, he only has
to look here<g>!! ANYway:

PHP 5.2.5; XP Pro SP2+, local Apache Server

My actual question is: How do I get a POST variable into a session
variable so I can use it in any other page I want to use it in?
Apparently posted variables are only available in the Form page in
the file called by Action=, right? But I'd like to use it in other
places.

By way of example, if this makes any sense:

I have a form.php that posts data to a-file.php. I would also like to
use that same var dat in files other than a-file.php though.
Since however it's only available to a-file.php because that's where
the form's action= sends it, I logically then cannot get the same
variable for z-file.php, right?

So the logical way to get it accessible for other files then would be to
put it into a session variable, right? HOW the heck do I do that???

In this particular case, to keep it simple, I've been using "age" for
the variable. I want to 'input' "age" in 1.php, and use it in both
2.php and 3.php.
===========
In 1.php I have:
<?php
session_start() ;
$_SESSION['age'] = $age;
?>
<form method="post" action="2.php"
<input type="text" name="age"
<input type="Submit" value="Continue ">
</form>
------------------
Runs OK. Say the entered age was "66".
==========

In 2.php I have:
<?php
session_start() ; // needed or $age fails completely.
$age = $_POST['age'];
echo '$age is ' . $age;
echo '<br>$POST is ' . $_POST['age'];
echo '<br>session age is ' . $_SESSION['age'];
echo "<br><br><H R>";
?>
<br>
<a href="3.php">Go to 3.php and run same code</a>
---------------------
Outputs:
$age is 66
$POST is 66
session age is

Go to 3.php and run same code

And throws this warning:

Warning: Unknown: Your script possibly relies on a session side-effect
which existed until PHP 4.2.3. Please be advised that the session
extension does not consider global variables as a source of data, unless
register_global s is enabled. You can disable this functionality and this
warning by setting session.bug_com pat_42 or session.bug_com pat_warn to
off, respectively. in Unknown on line 0

I prefer not to mod the .ini; I'd rather know the WHY.
============
and in 3.php I Have:
<?php
session_start() ;
$age = $_SESSION['age'];
echo '$age is ' . $age;
echo '<br>$POST is ' . $_POST['age'];
echo '<br>session age is ' . $_SESSION['age'];
?>
-----------------------
Clicking the link in 2.php results in this output:
$age is 66
$POST is
session age is 66
BUT there seems to be NO WAY I can find to get "age" properly into a
session variable in 2.php.
The code here is the closest I can come to getting it working; which
isn't very desirable, especially considering the warning, which is new
to me and no amount of research seems to explain it well to me so I can
understand WHY it throws that error.
=============

It's doing approximately what I want, but ... it doesn't feel right, and
I can't do anything to get rid of that Warning and still keep the code
working. Thus, I think I need to get 2.php working correctly.
Any and all relevant comments much appreciated by this newbie,

TIA,
Twayne

Jun 29 '08 #1
5 2208
Twayne wrote:
Hi,

If ever a newbie wants to know how much he has to learn yet, he only has
to look here<g>!! ANYway:

PHP 5.2.5; XP Pro SP2+, local Apache Server

My actual question is: How do I get a POST variable into a session
variable so I can use it in any other page I want to use it in?
Apparently posted variables are only available in the Form page in
the file called by Action=, right? But I'd like to use it in other
places.

By way of example, if this makes any sense:

I have a form.php that posts data to a-file.php. I would also like to
use that same var dat in files other than a-file.php though.
Since however it's only available to a-file.php because that's where
the form's action= sends it, I logically then cannot get the same
variable for z-file.php, right?

So the logical way to get it accessible for other files then would be to
put it into a session variable, right? HOW the heck do I do that???

In this particular case, to keep it simple, I've been using "age" for
the variable. I want to 'input' "age" in 1.php, and use it in both
2.php and 3.php.
===========
In 1.php I have:
<?php
session_start() ;
$_SESSION['age'] = $age;
?>
<form method="post" action="2.php"
<input type="text" name="age"
<input type="Submit" value="Continue ">
</form>
------------------
Runs OK. Say the entered age was "66".
==========

In 2.php I have:
<?php
session_start() ; // needed or $age fails completely.
$age = $_POST['age'];
echo '$age is ' . $age;
echo '<br>$POST is ' . $_POST['age'];
echo '<br>session age is ' . $_SESSION['age'];
echo "<br><br><H R>";
?>
<br>
<a href="3.php">Go to 3.php and run same code</a>
---------------------
Outputs:
$age is 66
$POST is 66
session age is

Go to 3.php and run same code

And throws this warning:

Warning: Unknown: Your script possibly relies on a session side-effect
which existed until PHP 4.2.3. Please be advised that the session
extension does not consider global variables as a source of data, unless
register_global s is enabled. You can disable this functionality and this
warning by setting session.bug_com pat_42 or session.bug_com pat_warn to
off, respectively. in Unknown on line 0

I prefer not to mod the .ini; I'd rather know the WHY.
============
and in 3.php I Have:
<?php
session_start() ;
$age = $_SESSION['age'];
echo '$age is ' . $age;
echo '<br>$POST is ' . $_POST['age'];
echo '<br>session age is ' . $_SESSION['age'];
?>
-----------------------
Clicking the link in 2.php results in this output:
$age is 66
$POST is
session age is 66
BUT there seems to be NO WAY I can find to get "age" properly into a
session variable in 2.php.
The code here is the closest I can come to getting it working; which
isn't very desirable, especially considering the warning, which is new
to me and no amount of research seems to explain it well to me so I can
understand WHY it throws that error.
=============

It's doing approximately what I want, but ... it doesn't feel right, and
I can't do anything to get rid of that Warning and still keep the code
working. Thus, I think I need to get 2.php working correctly.
Any and all relevant comments much appreciated by this newbie,

TIA,
Twayne
You never set $_SESSION['age']. You need something like:

$_SESSION['age'] = $_POST['age'];

Of course, like any user data, you should validate/sanitize it before
using it. The most logical place is before you put in the $_SESSION
variable.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jun 29 '08 #2
"Twayne" <no****@devnull .spamcop.netwro te:
>
My actual question is: How do I get a POST variable into a session
variable so I can use it in any other page I want to use it in?
Apparently posted variables are only available in the Form page in
the file called by Action=, right? But I'd like to use it in other
places.
There are several ways to do this. A $_SESSION variable is one way, and in
many cases that's the most convenient. However, if you have a bunch of
pages with <form>s that succeed one another, it's also possible to pass
this kind of information as <input type=hiddenvari ables without the
overhead of a session.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 30 '08 #3
..oO(Tim Roberts)
>"Twayne" <no****@devnull .spamcop.netwro te:
>>
My actual question is: How do I get a POST variable into a session
variable so I can use it in any other page I want to use it in?
Apparently posted variables are only available in the Form page in
the file called by Action=, right? But I'd like to use it in other
places.

There are several ways to do this. A $_SESSION variable is one way, and in
many cases that's the most convenient. However, if you have a bunch of
pages with <form>s that succeed one another, it's also possible to pass
this kind of information as <input type=hiddenvari ables without the
overhead of a session.
Bad idea. Sessions are made for this. It's the hidden fields that cause
the real overhead on the network and the server:

* The same data has to be transferred back and forth between the server
and the client multiple times, wasting bandwidth.
* You have to re-validate the same data over and over again on every
single form submission, because even hidden fields can be manipulated.

With a session you do it all just once: you receive the data, validate
it, store it in the session and that's it. The overhead caused by the
session handler is not really an issue.

Micha
Jun 30 '08 #4
..oO(sheldonlg)
>Tim Roberts wrote:
>>
There are several ways to do this. A $_SESSION variable is one way, and in
many cases that's the most convenient. However, if you have a bunch of
pages with <form>s that succeed one another, it's also possible to pass
this kind of information as <input type=hiddenvari ables without the
overhead of a session.

To expand in what Tim wrote, here is what you would do in 2.php

<input type="hidden" value="<?php print $_POST['age']; ?>" >
Such improper use of form values is one of the main reasons for XSS
attacks. You almost always want to use htmlspecialchar s() before the
output.

Micha
Jun 30 '08 #5
.oO(Tim Roberts)
>
>"Twayne" <no****@devnull .spamcop.netwro te:
>>>
My actual question is: How do I get a POST variable into a session
variable so I can use it in any other page I want to use it in?
Apparently posted variables are only available in the Form page in
the file called by Action=, right? But I'd like to use it in other
places.

There are several ways to do this. A $_SESSION variable is one way,
and in many cases that's the most convenient. However, if you have
a bunch of pages with <form>s that succeed one another, it's also
possible to pass this kind of information as <input type=hidden>
variables without the overhead of a session.

Bad idea. Sessions are made for this. It's the hidden fields that
cause the real overhead on the network and the server:

* The same data has to be transferred back and forth between the
server and the client multiple times, wasting bandwidth.
* You have to re-validate the same data over and over again on every
single form submission, because even hidden fields can be
manipulated.

With a session you do it all just once: you receive the data, validate
it, store it in the session and that's it. The overhead caused by the
session handler is not really an issue.

Micha
Hmm, recommended or not, that was at least educational. The validation
issue is what convinced me, I think.

I finally got my head around sessions and they aren't so confusing
now. Funny how these danged newfangled machines only do what you tell
them to do, not what you meant to do!! :-)

Thanks to all,

Twayne
Jul 2 '08 #6

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

Similar topics

11
3351
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option Explicit and Response.Expires=-1,
1
3113
by: Ann Leland | last post by:
I have been using session variables to pass a user name from one ASP page to another inside framesets for 9 months and it stopped working this week. I have made no code changes but there was a "security update" installed on the server a few days ago but I can't find out exactly what it was. In the research I have done I found many articles on the subject of session variables in ASP pages inside framesets. From what I read a new...
2
13557
by: Eric | last post by:
Hi, I've a problem with trying to retrieve a session variable in an include file. Basically, the main asp creates a session variable: <% Session("var1") = "Hello" %> And then when I click on a button it refers to the include file, which I believe is all client-side code as there are no server <% %> tags.
4
1660
by: VB Programmer | last post by:
If I have a variable I want to share in my application what is the difference between just declaring a variable (Dim strMyVar as String) and using a session variable (Session("strMyVar"))? When should I use a session variable and when should I just declare it like normal? Thanks in advance!
9
2454
by: William LaMartin | last post by:
I have a problem, mentioned here before, of Session and Application variables disappearing at one site but not at others or on my development computer. The problem is illustrated by an example at http://www.lamartin.com/dotnet/sessiontestset.aspx, were I set Session, Application and Cache variables on the first page and then on the second page view them as the second page is refreshed every five seconds. Before 10 refreshes, the...
9
2386
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use - perhaps I'm just not very smart or perhaps MS is making this too hard for us sql bunnies to understand - I dunno, but I'd really appreciate someone explaining what I'm doing wrong here & perhaps suggest a better approach.. I'm familiar with use of...
4
1953
by: T Ralya | last post by:
I am told that ASP.NET controls the session ID and session variables, but that does not fit my symptoms. I am posting here as directed. I'm hoping that someone can at least recommend something to try to isolate the problem. I have a simple application that demonstrates my problem. Page 1, step1: SaveSessionVariableButton will create a string value, show it on screen, save it in a session variable and show the session ID on screen....
3
2912
by: Alan Wang | last post by:
Hi there, Once my application gets complicated and complicated. I found it's really hard to keep track of Session value I am using in my asp.net application. I am just wondering if anyone have any experience on how to keep track of session value. Any help it's appreciated. Thanks Alan
4
3447
by: Don Miller | last post by:
I am using a Session variable to hold a class object between ASP.NET pages (in VB). In my constructor I check to see if the Session variable exists, and if it doesn't, I create one and populate it with "Me". But if the Session variable already exists, I would like the new object be populated with everything from the object stored in the Session variable. Ideally, I'd like to retrieve the object from the Session variable and assign it to...
17
5093
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any guidance appreciated. Regards
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.