![]() |
Setting temp cookies of 15secs
Hi. I would need to store in the client's browser a temp variable.
As asked HERE I tried different ways to do this, but never managed. Do you know some way to set and read a cookie within a function ? Actually I would need to know if an user, within a session, has done something. When he does it first, I could set a global/cookie var somewhere, but would know (and eventually change) that value from any php script. And that value should expire with that user's session... How could I do something like ? Thanks a lot. Bye |
Use PHP4's sessions, sounds perfect for what you're trying to do.
|
Excuse me but already checked that argument on php.net ...
And I didn't manage to implement and use a session var. Have you already done something about ? Thanks |
Nope, sorry, I haven't had much experience with sessions in PHP.
|
Thanks the same.
A general question: if I define a numeric variable in global.php, it should become a global (visible by all phps) one. For instance, PHP Code:
So, the same user should 'see' $myvar=1 ... Shouldn't it? And, for other users ? How will that var be valued ? Thanks |
Hi Jawelin,
Quote:
If you want to keep its new value, you have to save it somewhere (file, MYSQL etc.) Regards, Logician |
Perfect! It's exactly what I would for that variable! (read above)
This way, such variable should be valued per-user and per-session (I guess when that user's session expires, variable would loose its value... ???): exactly what I asked before to FireFly... Well. It doesn't work.... :( Whatever user saw that incremented variable (incremented within a function he runs), that variable is ALWAYS 0 ... :( (note: of course I globalized that variable within that function, even I tried to ++ it outside...) Bah! Thnx |
Quote:
1- "Global"ize the variable and return it back to script AFTER function ends. 2- Simpler way: define it as a static varible inside the function: static $a = 1; Then $a's value will be preserved when same user comes to the same function again. Of course this doesnt apply if the script is rerun! It's preserved until the script run ends. I think this is already what you wanted? |
I would that $a = 1 is mantained till that user is logged in that session.
I mean, if he reruns the same script after some time (let's say, two hours), that variable should be reset back to 0, before, then incremented. Is it what you said ? Thanks |
no, I meant 'static' command will preserve the variable value inside the function but only for that run of the script.
If you want to preserve the value for the user's session, you need to use it as a session variable: session_start(); //starts a session $id=session_id(); //your session id that you started $a=1; //your variable session_register("a"); //makes your variable a session var. and now call all your relevant scripts with "$id" (session variable) to keep your session (hence session variables). For example if a form returns some info back to your same script, use this: <form METHOD="POST" ACTION="yourscript.php?=$id"> By this way your $a is preserved for the entire session for this user.. Hope this helps.. |
Thank you very much for this example of session. I didn't understand too much from php.net... :D
Btw, I would investigate better the way of static var: if I call a function within a script, and in that function I had such a static var, that var will become available (and maintain the same value) for all the script execution time, is it true ? No matter if other scripts call the same function... they are different scripts, so that var will not keep the same value of the first script... Is it true ? Thanks again, foa for all the time you're spending with me... :D Thank you. |
ok let me summarize: :)
1- First thing to learn about PHP scripts is that: every script run is independent from others. This means that when I visit a thread here, "showthread.php" runs for ME and if you at the same time visit the same or an other thread same script will run for you TOO, but this running will be completely different from my running. (even if we click showthread.php just at the same EXACT second) Say, if my running the script change the variable of $a from 1 to 2, even if you run the script at the same time, $a variable would be "1" for you UNLESS your running changed its value for YOU too.. In other words everytime user A run a script, script will begin from the begining with default variable values. Others running of the scripts for other users would not effect variable values for user A. 2- Every run of the script (whether from the same user or not) is independent from eachother too. So if I run showthread.php to visit a thread, then click to an other thread in 2 seconds, another run of showthread.php will begin for me! They are not same so the variables will be reset again even if I'm the same user running the same script. That's why you use session variables, they keep their values after script is ended, until I close my session in your site and if I run the same script in the same session, session variables will keep their values.. 2- If a PHP script is not "included" or "required" inside another PHP script, these are completely different scripts and running of one does not effect other (applies to variables too). So for example if you assigned: $a=1; in global.php, $a would be 0 in showthread.php. It keeps its value in showthread.php too, JUST BECAUSE in the begining of that file global.php is "included". (So they become "one script"). Delete this line from showthread.php and you'll lose all variable values assigned in global.php... 3- GLOBAL usage in functions: $a = 1; function Test() { echo $a;} Test(); will print nothing. Because $a is a global (outside function)variable and $a in the function is different from $a outside the function.. $a = 1; function Test() { global $a; echo $a;} Test(); will print 1, because you now told your function to use global $a outside the function inside the function too.. 4- STATIC usage: Everytime a function runs, its variables (which is not "global"ized) get reset. Static helps to preserve their value inside the function, if the function is run again (inside the same script running! if same or another user runs the script again, variables reset remember?) function Test() { $b++;echo $b;} test() it will print 1 whenever you call the function test function Test() { static $b=0; $b++} Test(); it will print 1,2,3,4,5.. whenever you call the function test. Because static keeps the value of $b after function ends.. Hope this helps.. Logician |
Quote:
I'm gonna studying... :p Thanks a lot! Always look for a similar basic tutorial... Now I'll try to apply and later will let you know. Thanks again. Bye |
All times are GMT. The time now is 05:51 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|