1

I try to output my .php files from the command line and I want to be able to simulate $_SESSION values for this output.

I tried php test.php --define $_SESSION["id"]=1 but it does not set the global variable $_SESSION.

I am obviously not using the command line correctly but I cannot find how I should do.

Any idea to make it work?

Thanks a lot

EDIT: for my example test.php is the following:

<? echo $_SESSION['id'];?>
5
  • 1
    So you're not doing a session_start() anywhere in the php file? Commented Jun 19, 2012 at 21:28
  • Nope, I figured it would over ride a preset value of $_SESSION and wondered if I could use this global variable as a regular variable. I will try with it. Commented Jun 19, 2012 at 21:30
  • After testing, if I do session_start(), the $_SESSION variable is set to array(), which is not the cas if I do not do it (I checked with print_r. Everything looks like my --define does not do anything (or at least not what I expect) Commented Jun 19, 2012 at 21:33
  • What are you trying to accomplish by doing this? Commented Jun 19, 2012 at 21:34
  • I serve a different page if the user is logged in and if he is not (which I know from the $_SESSION variable). Now I am trying to output a static version of my connected page to build a native app with phone gap, that does not know PHP. I want to write a script that exports the logged in version of the page to a standard HTML I use in my phonegap app. Does it make sense? Any easier way to achieve this? Commented Jun 19, 2012 at 21:35

2 Answers 2

2
$ cat setsession.php 
<?php
//fixed session:
session_id("fixed");
session_start();
?>
$ cat checksession.php 
<?php
if(!isset($_SESSION['counter'])) $_SESSION['counter'] = 0;

$_SESSION['counter']++;

var_dump($_SESSION);
$ php -d auto_prepend_file=setsession.php checksession.php 
array(1) {
  ["counter"]=>
  int(1)
}
$ php -d auto_prepend_file=setsession.php checksession.php 
array(1) {
  ["counter"]=>
  int(2)
}
$ php -d auto_prepend_file=setsession.php checksession.php 
array(1) {
  ["counter"]=>
  int(3)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Works just like magic. It definitely makes more sense that all the shaky workarounds I tried so far.
0

You cannot use sessions on the command line in any meaningful way – the session identifier gets stored in a cookie. The command line doesn't do cookies, so the normal way of sessions doesn't work. Of course, you can use the $_SESSION superglobal during the run of the script. Just don't expect it to persist between different calls of the same script.

4 Comments

Thank you, your answer makes perfect sense but I did not expect any persistence. I just wanted to preset the $_SESSION variable (as any variable) before running the script. I tried -B and -d options but with no luck.
Ah, then I think I begin to understand. -d defines ini keys, not variables. php test.php -B '$_SESSION["abc"]="test";' might achieve what you want to be doing.
Thanks, I misunderstood -d however -B '$_SESSION["abc"]="test";' does not seem to be properly setting the variable, neither does -B '$_SESSION=array("abc"=>"test");', not sure why :-/
The -B would work equally well as the prepend file I proposed, but (1) we need a session-ident, (2) a session_start destroys a previously set $_SESSION, so, it would be -B 'session_id("fixedorsomething");session_start();$_SESSION["abc"]="test";'. I just prefer to put that stuff in a file so my command line isn't so cluttered (and I don't have to remember every setup, just use the same file next time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.