Jump to content


Photo

Keep Variables During Page Redirect


  • Please log in to reply
13 replies to this topic

#1 dst_u

dst_u
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 27 June 2006 - 06:22 PM

Hello,

I have several variables ($info1, $info2...) in one page that I want to be able to use in the new page to which I redirect using the header('Location: ' . $url) function.

Can you please explain to me how to keep these parameters valid for use in the next page?

Thanks,

David

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 June 2006 - 06:56 PM

The best way is using sessions. If you dont know what sessions are, read a tutorial about them. There are hundreds of tutorials.

You can also pass the variables in the url. But that's not recomended, especially if the information is sensetive (like passwords, credit card number etc'). But if you still choose this way, just do it this way:
header("Location: ".$url."info1=".$info1."&info2=".$info2); //etc'

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 27 June 2006 - 07:03 PM

like Orio suggested, use sessions. you can also [a href=\"http://www.php.net/serialize\" target=\"_blank\"]serialize[/a] your data before you pass it via sessions.

#4 dst_u

dst_u
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 27 June 2006 - 07:12 PM

Gentlemen,

Thank you for your prompt response.

I believe that a session is already open (I neglected to mention that I'm adding a feature to an existing code).

How can I know for sure if indeed a session is open and assuming that one already is, what should I do next?

I'm trying to read the sessions section in the php manual (http://www.php.net/m...ref.session.php), but it's a bit complicated for a newbie like me. I also didn't really succeed to understand the serialize function.

I can't use the url parameters because they are already used for other purposes.

Regards,

David

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 June 2006 - 07:51 PM

If session_start() was called, and the code has nothing like unset($_SESSION) or session_destroy(), that means you can use the $_SESSION array.
If session_start() wasnt called, you need to call it (in the begining of the script).
Then, you can set variables this way:
$_SESSION['var_name']=whatever

In the next page, you need to call session_start() again to recieve the variables you set before, and do whatever you want with them.

Here are a few tutorials about sessions:
Very basic- [a href=\"http://www.tizag.com/phpT/phpsessions.php\" target=\"_blank\"]http://www.tizag.com/phpT/phpsessions.php[/a]
Starts with basics and goes to more adavnced- [a href=\"http://codewalkers.com/tutorials/32/1.html\" target=\"_blank\"]http://codewalkers.com/tutorials/32/1.html[/a]
And you can search google for much more.


Orio
Think you're smarty?

(Gone until 20 to November)

#6 dst_u

dst_u
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 27 June 2006 - 08:43 PM

OK,

Since my previous response, I read a bit more and added the following lines to my code:
echo session_name() . "\n";
echo session_encode() . "\n";

the result was a page with many variables, among which I actually found the variables that I wanted to pass to the next page (they came in through the POST of the page).

The session_name seems to be "abcd" (and one of the variables I need is AccountNo), so I tried adding this line
echo abcd['AccountNo'];
to see if that's how I get the data, but after refreshing the page, I ended up with an empty page which seems to indicate that the syntax might be wrong.

Also, in the encode result, unlike the small examples in the php manual, there were many { and }, in addition to the names of variables, types and sizes, so it confused me a bit as to how to sort things out.

After that, I moved the 2 lines of code to the next page to which I want the info to arrive to see if I get the same session info, and the fields that I need are also present, which is a good thing.

I feel that I'm close, but I don't know how to proceed. All I need now is to be able to exctract this info and to associate it to another variable and I'm done.

Regards,

David


#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 June 2006 - 09:08 PM

No need to use both of these functions. Read this simple tutorial:
[a href=\"http://www.tizag.com/phpT/phpsessions.php\" target=\"_blank\"]http://www.tizag.com/phpT/phpsessions.php[/a]

Orio.
Think you're smarty?

(Gone until 20 to November)

#8 dst_u

dst_u
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 27 June 2006 - 09:18 PM

Hi Orio,

I read it (3 times, as it takes time for the concept to sink in :-) ), but when I tried to use $_SESSION, I didn't get anything either. I think that since the session has a name ('abcd' in my example), I should be probably use it, but I don't know how.

David

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 27 June 2006 - 11:18 PM

Try to understand this simple example ok good luck.

test.php
<?session_start();
//start the session

// varable name john
$name="john";

// varable age 20
$age="20";

// set  a session name to $user_name
$_SESSION['name']=$name;

//set session age to user_age
$_SESSION['age']=$age;

echo"<a href='test_results.php'>Test Session</a>";
?>

test_results.php

<? session_start();

echo "<br>my name is ".$_SESSION['name']." and i am ".$_SESSION['age']." ";

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 dst_u

dst_u
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 28 June 2006 - 11:41 AM

Hello again,

This is where I stand after masive exploration:
I have a session named '111111', so I can't use $_SESSION.

Inside this session, I have many variables, but the one I need is deep inside an Object variable.

This is the Object variable's format:
navigation|O:17:"navigationhistory":2:{
s:4:"path";a:2:{
i:0;a:4:{
s:4:"page";s:10:"previous.php";
s:4:"mode";s:3:"SSL";
s:3:"get";a:0:{}
s:4:"post";a:2:{
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--] s:11:"ReferenceNo";s:12:"617953126687";
s:8:[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]"AuthCode"[!--colorc--][/span][!--/colorc--];s:6:"0L5326";
[!--colorc--][/span][!--/colorc--] }
}
i:1;a:4:{
s:4:"page";s:20:"current.php";
s:4:"mode";s:3:"SSL";
s:3:"get";a:0:{}
s:4:"post";a:0:{
}
}
}
s:8:"snapshot";a:0:{
}
}


This object is built of paths that include arrays of pages information and a snapshot that I don't know what it represents, but it's not relevant for me.
Inside each path (named i) array there's another array that includes, amongs others the "post" variables the page got. These are the values that I need.

So, the question is: How do I exctract the value [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]0L5326[!--colorc--][/span][!--/colorc--] of the variable [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]AuthCode[!--colorc--][/span][!--/colorc--] that is inside the array post that is inside the array path where i = 1 and path is inside the navigation object, where page's value is previous.php?


I hope this all makes sense to you and someone could help build the equation.

At the end, I would like to have a variable $AuthCode get the value [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]0L5326[!--colorc--][/span][!--/colorc--].

Thanks,

David

#11 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 28 June 2006 - 04:28 PM

You're making it WAY too difficult. Read redarrow's example.

Orio.
Think you're smarty?

(Gone until 20 to November)

#12 dst_u

dst_u
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 28 June 2006 - 04:46 PM

This is not something I'm "making". This is data that is already generated by the application that I using.

All I need is to extract part of this data and use it in local variables.

The question is what is the syntax of a field in an array inside an array inside an array inside an object.

Thanks,

David

#13 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 28 June 2006 - 05:14 PM

Ok, back to your first question:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Hello,

I have several variables ($info1, $info2...) in one page that I want to be able to use in the new page to which I redirect using the header('Location: ' . $url) function.
Can you please explain to me how to keep these parameters valid for use in the next page?

Thanks,
David[/quote]

We'll use sessions.

So we got 3 pages:
1) HTML form
2) The form's action
3) The page redirected by the form (lets say a thank you page)


So let's make them:

1) HTML form- called index.php:
<form action="submit.php" method="POST">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit">
</form>

2) The form's action- called submit.php:
<?php
$first=$_POST['fname'];
$last=$_POST['lname']
session_start();
$_SESSION['first']=$first;
$_SESSION['last']=$last;
//Continue script- mail, add to db or whatever
header("Location: thankyou.php");
?>

3) The page redirected by the form- called thankyou.php:
<?php
session_start();
$first=$_SESSION['first'];
$last=$_SESSION['last'];
echo("<html><head><title>Thank you!</title></head><body>");
echo("Thank you ".$first." ".$last."!<br>Your form was submitted.");
echo("</body></html>");
?>


Now this script works prefectly fine, and use's very simple session knowledge.

Hope it helps.


Orio.
Think you're smarty?

(Gone until 20 to November)

#14 dst_u

dst_u
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 28 June 2006 - 05:31 PM

Hi Orio,

I appreciate the time you dedicate to helping me, but the question has changed in the mean time, thanks to you (since you told me about sessions).

I realized that this is not what I need, because a session already exists and the data that I'm looking to use in the "next page" is already there (inside the session).

Now, all I need is to "pull" this information out of the session into "local" variables.

As you can see in a post I made earlier, this information is not stored as a simple variable in the session, but it's inside an array that is inside another array that is inside an object, so now I need the syntax to use to extract this information out.

Also, the session's name is NOT $_SESSION, so this should also be taken into consideration.

Thanks,

David




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users