Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15611 > unrolled thread

PHP Global Variable Usage - Possible?

Started byMikeCopeland <mrc2323@cox.net>
First post2015-07-26 18:20 -0700
Last post2015-07-27 20:59 -0400
Articles 6 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  PHP Global Variable Usage - Possible? MikeCopeland <mrc2323@cox.net> - 2015-07-26 18:20 -0700
    Re: PHP Global Variable Usage - Possible? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-07-26 21:41 -0400
      Re: PHP Global Variable Usage - Possible? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-07-26 21:51 -0400
    Re: PHP Global Variable Usage - Possible? Matthew Carter <m@ahungry.com> - 2015-07-26 21:42 -0400
      Re: PHP Global Variable Usage - Possible? Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-27 23:00 +0000
        Re: PHP Global Variable Usage - Possible? Matthew Carter <m@ahungry.com> - 2015-07-27 20:59 -0400

#15611 — PHP Global Variable Usage - Possible?

FromMikeCopeland <mrc2323@cox.net>
Date2015-07-26 18:20 -0700
SubjectPHP Global Variable Usage - Possible?
Message-ID<MPG.301f35426b8e02f09896ae@news.eternal-september.org>
   Almost new to PHP, and I'm wondering if PHP will do something I want 
to do.  Scenario:
  1. 3 Web pages: index.php; text01.php; and people.php
  2. In index.php I want to establish a default value in a global 
variable, and I want to access that variable's value in test01.php
  3. In text01.php I have numerous divs and paragraphs of text.  I want 
to use the global value to establish a default text color and div 
background.  I also have anchor links in the code to allow other pages 
to link to specific areas of the text data in text01.php
  4. In people.php I have some hrefs that link in text01.php.  When 
someone clicks on those links in people.php, not only do I want 
text01.php rendered, but I want to be able to change the global 
variable's value so that the logic in text01.php can change 
characteristics of that div code in text01.php.
   (Note that I have the fundamentals of the Web site working - as .html 
pages - and I haven't yet invested in converting to using php sources.  
(I did some php Web development several years ago, but I've discarded 
that system and forgotten all of what I did: I'm starting over, and I 
never did anything like this current project...)
   Is such processing at all possible?  If so, please direct me to some 
examples of "how to" that I may start from.  TIA


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

[toc] | [next] | [standalone]


#15612

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2015-07-26 21:41 -0400
Message-ID<6%ftx.183040$1R3.14672@fx24.iad>
In reply to#15611
On Sunday July 26 2015 21:20, in comp.lang.php, "MikeCopeland"
<mrc2323@cox.net> wrote:

>    Almost new to PHP, and I'm wondering if PHP will do something I want
> to do.  Scenario:
>   1. 3 Web pages: index.php; text01.php; and people.php
>   2. In index.php I want to establish a default value in a global
> variable, and I want to access that variable's value in test01.php
>   3. In text01.php I have numerous divs and paragraphs of text.  I want
> to use the global value to establish a default text color and div
> background.  I also have anchor links in the code to allow other pages
> to link to specific areas of the text data in text01.php
>   4. In people.php I have some hrefs that link in text01.php.  When
> someone clicks on those links in people.php, not only do I want
> text01.php rendered, but I want to be able to change the global
> variable's value so that the logic in text01.php can change
> characteristics of that div code in text01.php.
>    (Note that I have the fundamentals of the Web site working - as .html
> pages - and I haven't yet invested in converting to using php sources.
> (I did some php Web development several years ago, but I've discarded
> that system and forgotten all of what I did: I'm starting over, and I
> never did anything like this current project...)
>    Is such processing at all possible?  If so, please direct me to some
> examples of "how to" that I may start from.  TIA

This sounds like a job for cookies and a "session".

At the start of each php script, make a call to session_start(). This will
establish (or re-establish) a session, in which information may be retained
page-to-page.

You treat the $_SESSION[] variables as your "global" variables, setting,
resetting, and testing them as necessary.

-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#15614

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2015-07-26 21:51 -0400
Message-ID<y8gtx.168083$Ml.14501@fx23.iad>
In reply to#15612
On Sunday July 26 2015 21:41, in comp.lang.php, "Lew Pitcher"
<lew.pitcher@digitalfreehold.ca> wrote:

> On Sunday July 26 2015 21:20, in comp.lang.php, "MikeCopeland"
> <mrc2323@cox.net> wrote:
> 
>>    Almost new to PHP, and I'm wondering if PHP will do something I want
>> to do.  Scenario:
>>   1. 3 Web pages: index.php; text01.php; and people.php
>>   2. In index.php I want to establish a default value in a global
>> variable, and I want to access that variable's value in test01.php
>>   3. In text01.php I have numerous divs and paragraphs of text.  I want
>> to use the global value to establish a default text color and div
>> background.  I also have anchor links in the code to allow other pages
>> to link to specific areas of the text data in text01.php
>>   4. In people.php I have some hrefs that link in text01.php.  When
>> someone clicks on those links in people.php, not only do I want
>> text01.php rendered, but I want to be able to change the global
>> variable's value so that the logic in text01.php can change
>> characteristics of that div code in text01.php.
>>    (Note that I have the fundamentals of the Web site working - as .html
>> pages - and I haven't yet invested in converting to using php sources.
>> (I did some php Web development several years ago, but I've discarded
>> that system and forgotten all of what I did: I'm starting over, and I
>> never did anything like this current project...)
>>    Is such processing at all possible?  If so, please direct me to some
>> examples of "how to" that I may start from.  TIA
> 
> This sounds like a job for cookies and a "session".
> 
> At the start of each php script, make a call to session_start(). This will
> establish (or re-establish) a session, in which information may be retained
> page-to-page.
> 
> You treat the $_SESSION[] variables as your "global" variables, setting,
> resetting, and testing them as necessary.

A quick, two-page example:

index.php
  <?php
    session_start();

    $_SESSION['gotsession'] = date("Y-m-d H:i:s");
  ?>
  <html>
  <head><title>Hello</title></head>
  <body><a href="page1.php">Take me to the next page</a></body>
  </html>

page1.php
  <?php
    session_start();
  ?>
  <html>
  <head><title>Page 1</title></head>
  <body>
  <?php printf("Session started at %s\n",$_SESSION['gotsession']); ?>
  </body>
  </html>


-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#15613

FromMatthew Carter <m@ahungry.com>
Date2015-07-26 21:42 -0400
Message-ID<87k2tmcr65.fsf@ahungry.com>
In reply to#15611
MikeCopeland <mrc2323@cox.net> writes:

>    Almost new to PHP, and I'm wondering if PHP will do something I want 
> to do.  Scenario:
>   1. 3 Web pages: index.php; text01.php; and people.php
>   2. In index.php I want to establish a default value in a global 
> variable, and I want to access that variable's value in test01.php
>   3. In text01.php I have numerous divs and paragraphs of text.  I want 
> to use the global value to establish a default text color and div 
> background.  I also have anchor links in the code to allow other pages 
> to link to specific areas of the text data in text01.php
>   4. In people.php I have some hrefs that link in text01.php.  When 
> someone clicks on those links in people.php, not only do I want 
> text01.php rendered, but I want to be able to change the global 
> variable's value so that the logic in text01.php can change 
> characteristics of that div code in text01.php.
>    (Note that I have the fundamentals of the Web site working - as .html 
> pages - and I haven't yet invested in converting to using php sources.  
> (I did some php Web development several years ago, but I've discarded 
> that system and forgotten all of what I did: I'm starting over, and I 
> never did anything like this current project...)
>    Is such processing at all possible?  If so, please direct me to some 
> examples of "how to" that I may start from.  TIA
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>

Variables only persist for the duration of script/program execution.

You may want to look into using cookies or sessions (or you could have
some persistence via the query string).

-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

[toc] | [prev] | [next] | [standalone]


#15615

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-07-27 23:00 +0000
Message-ID<mp6d61$j50$1@dont-email.me>
In reply to#15613
On Sun, 26 Jul 2015 21:42:10 -0400, Matthew Carter wrote:

> MikeCopeland <mrc2323@cox.net> writes:
> 
>>    Almost new to PHP, and I'm wondering if PHP will do something I want
>> to do.  Scenario:
>>   1. 3 Web pages: index.php; text01.php; and people.php 2. In index.php
>>   I want to establish a default value in a global
>> variable ....

> Variables only persist for the duration of script/program execution.

> You may want to look into using cookies 

Bad suggestion, the cookie gets passed to the user, they can change it. 
Server data persisted in cookies can provide attack vectors.

> or sessions 

Good suggestion, the only way the users can manipulate this is if they 
manage to hit on a valid session cookie other than their own when 
manipulating the cookie - sensible session cookie generation makes this 
highly unlikely.

> (or you could have some persistence via the query string).

Very bad suggestion, query string is even easier for attackers to 
manipulate than cookies.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#15616

FromMatthew Carter <m@ahungry.com>
Date2015-07-27 20:59 -0400
Message-ID<87pp3d5c7c.fsf@ahungry.com>
In reply to#15615
Denis McMahon <denismfmcmahon@gmail.com> writes:

> On Sun, 26 Jul 2015 21:42:10 -0400, Matthew Carter wrote:
>
>> MikeCopeland <mrc2323@cox.net> writes:
>> 
>>>    Almost new to PHP, and I'm wondering if PHP will do something I want
>>> to do.  Scenario:
>>>   1. 3 Web pages: index.php; text01.php; and people.php 2. In index.php
>>>   I want to establish a default value in a global
>>> variable ....
>
>> Variables only persist for the duration of script/program execution.
>
>> You may want to look into using cookies 
>
> Bad suggestion, the cookie gets passed to the user, they can change it. 
> Server data persisted in cookies can provide attack vectors.
>
>> or sessions 
>
> Good suggestion, the only way the users can manipulate this is if they 
> manage to hit on a valid session cookie other than their own when 
> manipulating the cookie - sensible session cookie generation makes this 
> highly unlikely.
>
>> (or you could have some persistence via the query string).
>
> Very bad suggestion, query string is even easier for attackers to 
> manipulate than cookies.

All true if the validity of the data needs to remain in-tact.  I just
skimmed his post and didn't really grok if he needed that, or just some
persistent state to his site.

If it's a persistent state that should be shared among users, the query
string is actually very useful, as friends can then send the link back
and forth.

-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web