Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How can I change the value of one registered variable?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How can I change the value of one registered variable?

Postby miguelna » Wed Oct 16, 2002 3:12 am

How can I change the value of one registered variable?
I tried
Code: Select all
session_register(myvar);
$_SESSION['myvar'] = $myvar1;


I want a variable to be present in all pages. Is this the best way?

Thanks,

M
miguelna
New php-forum User
New php-forum User
 
Posts: 15
Joined: Wed Sep 04, 2002 12:53 am

Postby DoppyNL » Wed Oct 16, 2002 3:17 am

I find it the best way to use session variables is to allways acces them via the global array.
even when first creating the variable, so I don't do this:
Code: Select all
session_register('myvar');
$myvar = 12;

but:
Code: Select all
session_register('myvar');
$_SESSION['myvar'] = 12;

Even when I give it is value for the first time, that way you only use 1 method of accesing the variable wich is much easyer to understand and makes you're code easyer to read.

once the variable is registerd you can allways acces it via the global arrays and change it via the global arrays, its that easy.

Greetz Daan
DoppyNL
 

Postby miguelna » Wed Oct 16, 2002 3:34 am

I made this, but the value of the registered variable remains the same. The value myvar1 does not pass to myvar.
miguelna
New php-forum User
New php-forum User
 
Posts: 15
Joined: Wed Sep 04, 2002 12:53 am

Postby DoppyNL » Wed Oct 16, 2002 4:05 am

I asume you did use session_start() before accesing you're session variables on the next page?

You could also check if session is enabled on you're php-installation.
make a file with these contents:
Code: Select all
<?php phpinfo(); ?>

under "session" it should say that it is enabled.

also, if you don't accept cookies and php isn't compiled with "--enable-trans-sid" you will have to make sure sessions are transfered to the next page via the url, otherwise you lose you're session because the next page doesn't know that you're allready in a session and will start a new one.
phpinfo() also displays the compile options of you're php-installation.

Greetz Daan
DoppyNL
 

Postby miguelna » Wed Oct 16, 2002 4:20 am

One question: What is the best way to declare a global variable to be always present in the page?
I call this page with page1.php?a=2$b=5
But I want the vars c and d, previous declared in the page to print them.
miguelna
New php-forum User
New php-forum User
 
Posts: 15
Joined: Wed Sep 04, 2002 12:53 am

Postby DoppyNL » Wed Oct 16, 2002 4:30 am

Code: Select all
page1.php?a=2$b=5

I think this should be:
Code: Select all
page1.php?a=2&b=5

because that is default (unless you changed it ofcourse :)

I don't get what you mean exactly.

Do you want to use a constant? --> Function define

Do you want to transfer variables from script to script and change them whenever you want? --> Session variables.

Something else??

Greetz Daan
DoppyNL
 

Postby Jay » Wed Oct 16, 2002 1:22 pm

Why are you guys using the session_register('myvar'); line? You don't need it after 4.1.0 (I think, I know def after 4.2.0).
Jay
 

Postby DoppyNL » Wed Oct 16, 2002 11:15 pm

As far as I know session_register has to be called to remember a variable in a session. The manual doesn't mention that this is no longer needed. So I still use it.

Greetz Daan
DoppyNL
 

Postby Jay » Thu Oct 17, 2002 3:25 am

DoppyNL wrote:As far as I know session_register has to be called to remember a variable in a session. The manual doesn't mention that this is no longer needed. So I still use it.

Greetz Daan

No, you just need to start the session. If you use the SGA, by doing $_SESSION['var'] = $var you are impliedly declaring it as a session variable (ie PHP will now do it for you).
Jay
 

Postby DoppyNL » Thu Oct 17, 2002 3:28 am

8) Cool! 8)

btw, since wich version? So I know when it works and when not :)

Greetz Daan
DoppyNL
 

Postby Jay » Thu Oct 17, 2002 3:32 am

AFAIK any version which has got the SGA as long as you're using them, so I would imagine 4.1.0 onwards. I only knew from 4.2.0 because they enforced the SGA, but in theory it should work from 4.1.0 because that's when they were introduced
Jay
 


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron