PHP writing variables and string to text file -


i'm trying write file this:

<?php     date_default_timezone_set('europe/budapest');      if(isset($_post['user'])) {         global $user;         $user = $_post['user'];     } else {         die("nincs user beállítva!");     }     if(isset($_post['pass'])) {         global $pass;         $pass = $_post['pass'];     } else {         die("nincs pass beállítva!");     }      if(!isset($_post['msg'])) {         die("nincs üzenet!");     } else {         global $msg;         $msg = $_post['msg'];     }      if(!file_exists("logfile.txt")) {         die("nem létezik logfile.txt!");     }     $cont = file_get_contents("logfile.txt");     file_put_contents("logfile.txt","{$user}: {$msg}\n{$cont}"); //<-- tried 1 many ways ?> 

and gives me in txt file:

<? global $user; echo $user; ?>: test 

no matter change in file_put_contents, give similar this. in advance.

edit: made edit @barmar suggested, still doing same thing:

<form name="send" action="chat_send.php" method="post">     <input type="text" name="msg" autocomplete="off" value="">     <?php     global $user;     echo '<input type="hidden" name="user" value="' . $user . '">';     ... </form> 

there's nothing wrong how you're writing file. problem how you're setting $_post['user']. looks me script created form did like:

echo '<input type="hidden" name="user" value="<?php global $user; echo $user; ?>">'; 

you can't use <?php ... ?> in middle of string execute php code; that's used when you're outputing normal html after ?>, php execution mode temporarily. form contains literal string ?php global $user; echo $user; ?> in hidden input value.

in string, use concatenation, should be:

global $user; echo '<input type="hidden" name="user" value="' . $user . '">'; 

or can return html mode first:

?> <form name="send" action="chat_send.php" method="post">     <input type="text" name="msg" autocomplete="off" value="">     <input type="hidden" name="user" value="<?php global $user; echo $user; ?>">     ... </form> <?php 

Comments