# The scope of $\_SESSION

**URL:** <https://www.phphelp.com/t/the-scope-of-session/33195>\
**Category:** General PHP Help\
**Created:** [February 28, 2021, 3:50am UTC](https://www.phphelp.com/t/the-scope-of-session/33195 "2021-02-28T03:50:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedroski55](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@Pedroski55](https://www.phphelp.com/u/Pedroski55)\
**Post date:** [February 28, 2021, 3:50am UTC](https://www.phphelp.com/t/the-scope-of-session/33195/1 "2021-02-28T03:50:41Z")

</div>

I have a simple little homework page. It begins with an index.php, which points directly at startfile.html.php

At the top of startfile.html.php I start a $\_SESSION array

\<?php session\_start(); $\_SESSION['error'] = 'So far everything is OK.'; ?\>

I thought this was a global array, available to all webpages beneath startfile.html.php i.e. all my folders and webpages.

However, when I do not have session\_start(); at the top of, say change\_PW\_form.php I do not see the error messages from $\_SESSION.

For instance:  
if($password != $confirm){  
//return the values to the user  
$\_SESSION[‘email’] = $email;  
//display error  
$\_SESSION[‘PWerror’] = ‘密码不一样 Passwords did not match’;  
include ‘changePW\_form.php’;  
exit();  
}

If I enable session\_start(); at the top of all my pages, I see all the error or success messages.

BUT, then I constantly get error messages in the error\_log file like this:

> [Sun Feb 28 11:05:39 2021] [warn] [client 112.23.178.17] mod\_fcgid: stderr: PHP Notice: A session had already been started - ignoring session\_start() in /var/www/vhosts/mywebpage.com/httpdocs/test19BEcw/changePW\_form.php on line 3, referer: [https://mywebpage.com/test19BEcw/changePW.php](https://mywebpage.com/test19BEcw/changePW.php)

What can I do to stop these error\_log messages, but keep my problem or success info messages??

Maybe I need to declare a default value for all possible $\_SESSION values right at the start, not per folder and webpage??

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [February 28, 2021, 8:40am UTC](https://www.phphelp.com/t/the-scope-of-session/33195/2 "2021-02-28T08:40:01Z")

</div>

$\_SESSION variables are available throughout the ‘currently’ executing script, which would include files that are ‘required’ (you should use require for files that your script must have for it to work) through the filesystem by the currently executing script and any additional (nested) require statements in the required files. $\_SESSION variables persist between different http(s) requests when a successful session\_start() statement is executed.

Each web page needs one session\_start() statement. The error you are getting occurs when there is more than one, which would indicate that your main .php file contains one and the file being included (should use require) also contains one.

The symptom you are getting is likely (it would take seeing all the code needed to reproduce the problem) because you are both including a file and causing a http(s) request to the same file, such as redirecting to it, using it as the action in a form tag, or in a link.

---

<div class="post-metadata">

**Author:** ![Pedroski55](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@Pedroski55](https://www.phphelp.com/u/Pedroski55)\
**Post date:** [February 28, 2021, 10:47am UTC](https://www.phphelp.com/t/the-scope-of-session/33195/3 "2021-02-28T10:47:59Z")

</div>

Thanks!

So my toplevel file is: startfile.html.php with the PHP:

\<?php session\_start(); $\_SESSION['error'] = 'So far everything is OK.'; ?\>

I should put require (or include???) statements for each file used below it? Like:

```
 <?php session_start(); $_SESSION['error'] = 'So far everything is OK.'; 
require '/19BEcw/changePW_form.php'
require '/20BEcw/changePW_form.php'
// and quite a few more ...
?>
```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [February 28, 2021, 7:19pm UTC](https://www.phphelp.com/t/the-scope-of-session/33195/4 "2021-02-28T19:19:40Z")

</div>

> [@Pedroski55](#):
>
> I should put require (or include???) statements for each file used below it?

No. Nothing I wrote had anything to do with making changes to your code. I listed one likely cause, based on the symptoms, that has to be either confirmed or disproven as the source of the problem. You have to find what’s causing a problem before you can fix it.
