0

I want to enter the following code into the fwrite() function

"<?php 
session_start();
if ($_SESSION['currentuser'] === $username){
echo 'Logged in!';
}else{
echo 'You need to login!';
}?>"

but it shows me the T_ENCAPSED_AND_WHITESPACE error for the line if ($_SESSION['currentuser'] === $username){

can anybody help me with this.

  • Show how you're actually trying to write this – Mark Baker Oct 22 '12 at 08:45
  • 5
    I could give you the literal answer to the question, but I'm much more worried why you're trying to write this code into some file in the first place. Sounds like you're going off into a terrible direction with whatever problem you're really working on here. – deceze Oct 22 '12 at 08:46
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – miken32 Apr 16 '19 at 19:39

2 Answers2

2

This doesn't look like something that should be written to disk, but I won't judge...

You probably don't want any variable substitution, so you could use the NOWDOC syntax in PHP 5.4:

$var = <<<'EOD'
<?php 
session_start();
if ($_SESSION['currentuser'] === $username){
echo 'Logged in!';
}else{
echo 'You need to login!';
}?>
EOD;

fwrite($handle, $var);
CAMason
  • 1,122
  • 7
  • 13
  • A Resource object that you opened with `fopen()` or similar. – CAMason Oct 22 '12 at 11:02
  • basically what i wish to do is open a new php file with $username.php everytime a user signsup. i want to write that code to all the new files, but the $_SESSION shows an error. i want to allow access to $username.php only if the session created at login matches the username – jai4201 Oct 22 '12 at 11:18
2

You can try

$data ='<?php 
session_start();
if ($_SESSION[\'currentuser\'] === $username){
echo "Logged in!";
}else{
echo "You need to login!";
}?>' ;

file_put_contents("file.php", $data);
Baba
  • 94,024
  • 28
  • 166
  • 217