vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   Setting temp cookies of 15secs (https://vborg.vbsupport.ru/showthread.php?t=39007)

Jawelin 05-24-2002 05:08 PM

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

Admin 05-25-2002 04:52 AM

Use PHP4's sessions, sounds perfect for what you're trying to do.

Jawelin 05-25-2002 06:46 AM

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

Admin 05-25-2002 07:06 AM

Nope, sorry, I haven't had much experience with sessions in PHP.

Jawelin 05-27-2002 09:49 AM

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:

$myvar=0

Well. If I increment it in a certain script, shall it result increased also from all the other scripts ?
So, the same user should 'see' $myvar=1 ... Shouldn't it?

And, for other users ? How will that var be valued ?

Thanks

Logician 05-27-2002 10:28 AM

Hi Jawelin,

Quote:

Originally posted by Jawelin
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:

$myvar=0

Well. If I increment it in a certain script, shall it result increased also from all the other scripts ?
So, the same user should 'see' $myvar=1 ... Shouldn't it?

And, for other users ? How will that var be valued ?

everytime a PHP script is run by a user, it runs with default variable values. So if you change a variable value inside a script, change will apply only to user who triggered the script, not to other users running the same script.

If you want to keep its new value, you have to save it somewhere (file, MYSQL etc.)

Regards,
Logician

Jawelin 05-27-2002 10:53 AM

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

Logician 05-27-2002 02:33 PM

Quote:

Originally posted by Jawelin
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...)

If the variable is defined inside a function (and you want its value preserved when function (but not the script, "function"!) is run again), you have 2 choices:

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?

Jawelin 05-28-2002 03:57 PM

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

Logician 05-28-2002 04:15 PM

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..

Jawelin 05-28-2002 08:10 PM

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.

Logician 05-29-2002 08:27 AM

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

Jawelin 05-29-2002 09:44 AM

Quote:

Originally posted by Logician
ok let me summarize: :)
[...]
Hope this helps..
Logician

Wow!!! What a lesson!
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
  • Page Generation 0.01162 seconds
  • Memory Usage 1,756KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (13)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete