RGriffiths RGriffiths - 3 months ago 16
PHP Question

php include is not working as expected

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?

Answer

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