473,804 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Sessions to pass a value from one page to another in PHP

21 New Member
I use PHP sessions to pass a value from one page to another.



In my 1st page I pass a value called 'id' to my 2nd page using a hyperlink.

explained:

<a href="abc.php?i d=<?php print"$image_id ";?>"></a>

so in my 2nd page (abc.php) I capture the value for id using $_GET.

I want to know how I can do the same using session variables.

coz when I navigate away from abc.php and back it seems that the value in 'id' have disapeared.

I tried using this but doesnt seem to work. :(

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php session_start();
  3. session_start();    
  4. session_register ("pid");
  5. $pid = $_GET['id']; 
  6. $HTTP_SESSION_VARS ["pid"] = $pid;
  7. ?>
  8.  

Thanks in advance and pls help soon. I'm totally stuck without this.

By the way I hp I didnt giv a heartattack to the moderators again!
Aug 12 '08 #1
3 14268
coolsti
310 Contributor
This should work better for you.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. session_start();
  4. $pid = $_GET['id']; 
  5. $_SESSION["pid"] = $pid;
  6.  
  7. ?>
  8.  
Unless you have a very old php installation, the above should work instead of the code you presented. This starts the session, then puts the value for the id in a PHP variable $pid, and in the next line, it creates a session variable (by adding an element to the $_SESSION array) with key "pid" and sets it to the value of the id stored in $pid. Note that these two lines can be combined, to $_SESSION["pid"] = $_GET['id'] without needing to use the intermediate variable $pid.

You may want to call trim() on the value of id first, to get rid of any leading or trailing spaces in case they occur.

Note that you also need to call session_start() in any later pages that wish to access the value of $_SESSION["pid"].
Aug 12 '08 #2
nse111
21 New Member
This should work better for you.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. session_start();
  4. $pid = $_GET['id']; 
  5. $_SESSION["pid"] = $pid;
  6.  
  7. ?>
  8.  
Unless you have a very old php installation, the above should work instead of the code you presented. This starts the session, then puts the value for the id in a PHP variable $pid, and in the next line, it creates a session variable (by adding an element to the $_SESSION array) with key "pid" and sets it to the value of the id stored in $pid. Note that these two lines can be combined, to $_SESSION["pid"] = $_GET['id'] without needing to use the intermediate variable $pid.

You may want to call trim() on the value of id first, to get rid of any leading or trailing spaces in case they occur.

Note that you also need to call session_start() in any later pages that wish to access the value of $_SESSION["pid"].



Hey thanks so much for your answer. but even this way i keep loosing the value when I navigate to another page and back from abc.php.

Well im thinkin may be cookies is the solution

like:

setcookie("cook name", $_SESSION["pid"], time()+60*60*24 *100, "/");

if there's someother way smone pls let me know. thanks in advance!!


See what I want is to have a constant value for 'id' throughout my PHP pages.
I want the value for 'id' to be consistent throughout my pages (until the user selects another id)
Aug 12 '08 #3
bnashenas1984
258 Contributor
Hi
Just make sure you use session_start() at the begining of both pages.

I don't see anything wrong with this code.

And there is one other possiblity. PHP should have complete access to the folder it stores the sessions on.
Sep 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1886
by: Eddy C | last post by:
Hi, I'm trying to get the value of another node using the position of another node or the name of the tag. Such that the current node is one of the contacts child nodes sec or prim and doing <xsl:value-of select="//cust/name"/>
2
1939
by: xenophon | last post by:
I have specified in web.config to use cookieless Sessions. On one page I do not want to use them because of the way they obfuscate the page address. How can I turn it off for the page? Thanks.
17
7081
by: Rabbit | last post by:
Hi, On my 1st page, i have a function which gets a new ID value and need to transfer to another immediately. which I want to get in 2nd page using Request.form("txtID"), but doesn't work, the returned value is "" Anybody know how to set this value on 1st page properly, in order to let 2nd page catch it? I don't want to use querystring to pass this value!
3
2626
abdoelmasry
by: abdoelmasry | last post by:
Hi Friends i need help in sessions im passing variables between pages using sessions this is the main page thats mean the home page the main page have table to show messages for users , it have also the site style <? session_start(); echo
3
3233
by: valerehorath via AccessMonster.com | last post by:
Hi, I am attempting to select a value in a combo box in a form in access 2000 and pass it to a variable in an html document. How can i do this? Thank you very much. -- Message posted via http://www.accessmonster.com
11
16334
by: dkate777 | last post by:
Hi, Is anybody know how to pass textbox input value to another page. I am using document.secondform.fieldname.value=document.firstformname.fieldname.value but it's not working Thanks
7
2582
by: Boki | last post by:
Hi All, I can't pass data to another form: in form2: private void button1_Click(object sender, EventArgs e) { Form1 form_copy = new Form1();
2
1268
by: CharlesHawk | last post by:
HI i am working on a Project where i have to send a string using Sessions to the next page this is working gud for now But i can convert session to a string the .ToString(); and the Convert.ToString(.....); is not workin I am using Web Matrix . Please Help me ASAP. tanx
1
1192
by: boorama | last post by:
Hi all, I am new to php and trying to desing a booking system. the problem I have is how to retrive the session variable to use in a query. the user logs in with a reference number, I pass the number to the next page to display details that are specific to that user. I am using sessions.
4
21236
by: bluepiper | last post by:
Im using VB 6.0 The problem is have two forms the the first one (name it Form1) include one textbox control and button , and when pressing button I want to pass the textbox value to another text control in the second form (name it Form2). Please help me. Thanks
0
9705
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
9575
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
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7609
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
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
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
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
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
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.