Session passing over to a next page in PHP7

I have the code below at a page.

<?php

$myDomain='myDomain.com';
session_set_cookie_params(0, '/', '.' . $myDomain.'');
session_start();

if ( isset($_SESSION['mySession']) ) {

echo 'mySession is already set';

} else {

echo 'mySession will be set';
$_SESSION['mySession']=1;

}

If I open the page for the first time, it says, “mySession will be set”.
and I open the page again, it says “mySession is already set”.
So far so good.
But it is in the PHP5.

I have another server which is in the PHP7.
Same code is in the PHP7.

Whenever I open the page, it continuously says “mySession will be set”.
Although “mySession” is set, it seems not pass over to the next openning.

How can I make “mySession” pass over to a next page?

I’ve got a local PHP 7.2 server with php -S localhost:8080 running, works as expected.

My php is also 7.2.

Do you mean it says “mySession is already set” from the 2nd time if you run the code above?

Try adding the following and see if errors or warnings are generated:

<?php 
declare(strict_types=1);
errot_reporting(-1);
ini_set('display_errors', '1');

session_start();
//
<?php

declare(strict_types=1);
errot_reporting(-1);
ini_set('display_errors', '1');

$myDomain='myDomain.com';
session_set_cookie_params(0, '/', '.' . $myDomain.'');
session_start();

if ( isset($_SESSION['mySession']) ) {

echo 'mySession is already set';

} else {

echo 'mySession will be set';
$_SESSION['mySession']=1;

The code above says " Fatal error : Uncaught Error: Call to undefined function errot_reporting() on line 4

I try to remove the code below form the original code.

$myDomain='myDomain.com';
session_set_cookie_params(0, '/', '.' . $myDomain.'');

And make the code below. And it works fine.

<?php

session_start();

if ( isset($_SESSION['mySession']) ) {

echo 'mySession is already set';

} else {

echo 'mySession will be set';
$_SESSION['mySession']=1;

}

As the code above works fine, I can think the code below is the problem.

$myDomain='myDomain.com';
session_set_cookie_params(0, '/', '.' . $myDomain.'');

I added the problem code above for sub-domains.
In order to make the sessions and cookies works as same to all sub-domains of myDomain.com, for example, sub1.myDomain.com, sub2.myDomain.com.

With the problem code above, a session or cookie work not only myDomain.com but also sub1.myDomain.com and sub2.myDomain.com in PHP5.
But it doesn’t work as I expected in PHP7.

Can I make it work with cross-domain in PHP7

Maybe error_reporting(-1) :biggrin:

1 Like

I cannot edit this.
I guess it’s because of time passing.

I modify the quote above like the following.

The code1 below says " Fatal error : Uncaught Error: Call to undefined function errot_reporting() in sessionTest:4 Stack trace: #0 {main} thrown on line 4
code1

<?php

declare(strict_types=1);
errot_reporting(-1);
ini_set('display_errors', '1');

$myDomain='myDomain.com';
session_set_cookie_params(0, '/', '.' . $myDomain.'');
session_start();

if ( isset($_SESSION['mySession']) ) {

echo 'mySession is already set';

} else {

echo 'mySession will be set';
$_SESSION['mySession']=1;

And the code2 below which is without the semicolon(:wink: says " Parse error : syntax error, unexpected ‘ini_set’ (T_STRING) in sessionTest.php on line 5
code2

<?php

declare(strict_types=1);
errot_reporting(-1)
ini_set('display_errors', '1');

$myDomain='myDomain.com';
session_set_cookie_params(0, '/', '.' . $myDomain.'');
session_start();

if ( isset($_SESSION['mySession']) ) {

echo 'mySession is already set';

} else {

echo 'mySession will be set';
$_SESSION['mySession']=1;

How can I remove the Fatal error or Parse error?

Try @Gandalf’s solution In a previous post.

The syntax was not validated and typed incorrectly on my iPad.

Do you mean the code1 below should be modified to the code2 below?
code1

declare(strict_types=1);
errot_reporting(-1);
ini_set('display_errors', '1');

code2

declare(strict_types=1);
errot_reporting(-1)
ini_set('display_errors', '1');

I guess this part below has possibly incorrect.

session_set_cookie_params(0, '/', '.' . $myDomain.'');

Read post #6

Thank you.
I am afraid I made typo there.

So the code below is correcting the typo.

<?php

declare(strict_types=1);
error_reporting(-1);

ini_set('display_errors', '1');

$myDomain='dot.kr';
session_set_cookie_params(0, '/', '.' . $myDomain.'');
session_start();

if ( isset($_SESSION['mySession']) ) {

echo 'mySession is already set';

} else {

echo 'mySession will be set';
$_SESSION['mySession']=1;

It says “mySession will be set” continuously.
It means the session doesn’t pass over to the next page.

How can I make it the session pass over to the next page?
How can I see the quote below as the result?

Is the code actually running at dot.kr, or somewhere else? (like localhost)

Also, this:

$myDomain='dot.kr';
session_set_cookie_params(0, '/', '.' . $myDomain.'');

is a lot harder to understand than this:

session_set_cookie_params(0, '/', '.dot.kr');

The code below works fine, I can see “mySession is already set”
Thank you, rpkamp.

<?php

declare(strict_types=1);
error_reporting(-1);

ini_set('display_errors', '1');

session_set_cookie_params(0, '/', '.dot.kr');
session_start();

if ( isset($_SESSION['mySession']) ) {

echo 'mySession is already set';

} else {

echo 'mySession will be set';
$_SESSION['mySession']=1;
}

By the way, dot.kr is activated when I test it

When I test it, the server connected to real IP.
But mostly the server is connected to a router for internet connection.
That is one reason why my test is so slow.

The 3 code below don’t work correctly.

session_set_cookie_params(0, '/', '.localhost');
session_set_cookie_params(0, '/', '.127.0.0.1');
session_set_cookie_params(0, '/', '.218.160.156.29');

The code below works fine.

session_set_cookie_params(0, '/', '.dot.kr');

I think I have to find a way “dot.kr” is always activated,

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.