-1

I am trying to put an include into my php page but I keep getting an error. This works fine:

<?php
session_start();
$_SESSION['LAST_ACTIVITY'] = time();
etc etc

But if I put this into an include:

<?php
session_start();
include ('sessiontimer.php');
etc etc

with sessiontimer.php being:

<?php
echo "$_SESSION['LAST_ACTIVITY'] = time();";
?>

and I get the error:

PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in sessiontimer.php on line 2

Is there some rule about including time() or there something else I am missing?

RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • `echo "$_SESSION['LAST_ACTIVITY'] = time();";` why do you have that in quotes? that's why you're getting the error and the extra `;` also. – Funk Forty Niner Aug 13 '16 at 00:15
  • Since `$_SESSION['LAST_ACTIVITY'] = time();` works, your include should include just that, no quotes. Yet, I don't quite know what you want to do here. – Funk Forty Niner Aug 13 '16 at 00:17
  • As @Fred-ii- states, you need to clarify what you want doing, however I have put two solutions depending on what you want doing, they may or may not help you. – Script47 Aug 13 '16 at 00:20
  • The rule you're missing is about the syntax for putting variables inside strings. – Barmar Aug 13 '16 at 00:20
  • I think his confusion is that he thinks that `include` is like accessing the file through the web server: it executes the script remotely and then merges the output into the current script. – Barmar Aug 13 '16 at 00:21

1 Answers1

1

If you want to display that in a string, try the following,

<?php

echo "\$_SESSION['LAST_ACTIVITY'] = time();";

?>

Output

$_SESSION['LAST_ACTIVITY'] = time()

If you wish to set the session variable, try the following,

<?php

$_SESSION['LAST_ACTIVITY'] = time();

?>

Reading Material

Variable Parsing

Script47
  • 14,230
  • 4
  • 45
  • 66
  • If as you think they want to literally output `$_SESSION['LAST_ACTIVITY'] = time();` as a string, you forgot the `;` and \ ;-) Therefore, `echo "\$_SESSION['LAST_ACTIVITY'] = time();\";` – Funk Forty Niner Aug 13 '16 at 00:24
  • @Fred-ii- added the `;` however it output without the slash at the end too. Good spotting. – Script47 Aug 13 '16 at 00:25
  • Thanks for the pointers. Being a bit of a dope, echoing when I didn't need to. Cheers. – RGriffiths Aug 13 '16 at 00:26
  • @RGriffiths I for one, still don't know what it is you wanted to do. and posted a comment up there. – Funk Forty Niner Aug 13 '16 at 00:26
  • @Fred-ii- I am setting up a session variable called LAST_ACTIVITY. There will be other stuff going into the include which checks whether there has been any activity on a page or should it destroy the session and log you out. I didn't include that bit (excuse the pun). Now the include works I can just add the include to every page rather than copying the whole thing. – RGriffiths Aug 13 '16 at 00:29