Jump to content

Archived

This topic is now archived and is closed to further replies.

dst_u

Keep Variables During Page Redirect

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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/manual/en/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

Share this post


Link to post
Share on other sites
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:
[code]$_SESSION['var_name']=whatever[/code]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Try to understand this simple example ok good luck.

test.php
[code]
<?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>";
?>
[/code]

test_results.php
[code]

<? session_start();

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

?>
[/code]

Share this post


Link to post
Share on other sites
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:
[i]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:{
}
}[/i]

This object is built of [b]paths [/b]that include arrays of pages information and a [b]snapshot [/b]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--][b]0L5326[/b][!--colorc--][/span][!--/colorc--] of the variable [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--][b]AuthCode[/b][!--colorc--][/span][!--/colorc--] that is inside the array [b]post [/b]that is inside the array [b]path [/b] where i = 1 and [b]path [/b]is inside the [b]navigation [/b]object, where [b]page[/b]'s value is [b]previous.php[/b]?


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 [b]$AuthCode[/b] get the value [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--][b]0L5326[/b][!--colorc--][/span][!--/colorc--].

Thanks,

David

Share this post


Link to post
Share on other sites
You're making it WAY too difficult. Read redarrow's example.

Orio.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:
[code]<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>[/code]

2) The form's action- called submit.php:
[code]<?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");
?>[/code]

3) The page redirected by the form- called thankyou.php:
[code]<?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>");
?>[/code]


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

Hope it helps.


Orio.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.