How to put php code in fwriter

0

I'm doing a mini system to create a folder with files inside, but the html works perfectly. but when you put the php code, everything stops working. I can not find any information that can help me.

If anyone knows please help me :(

Or if I'm doing it wrong the code please tell me: (

Code:

?php
$fp = fopen("users/${dirname}/validar.php", "w") or die("Error al intentar abrir el archivo!");
fwrite($fp, "<?php

$miuser = 'ChristianHz';
$mipass = 'seraz90';

if(isset($_POST['login'])) {
	$usuario = $_POST['usuario'];
	$pass = $_POST['password'];
	if ($usuario == $miuser and $pass == $mipass ) {
        if (isset($_POST['remember'])) {
        	setcookie('usuario', $usuario, time()+60*60*7);
        	setcookie('passowrd', $pass, time()+60*60*7);
        } 
        session_start();
        	$_SESSION['usuario'] = $usuario;
        	header('location:panel');
    } else {
    	echo '<p>usuario o clave son incorrectos</p>';
    }
} else {
	header('location: login');
}

?>");
fclose($fp);
?> 
    
asked by danielmeza 31.12.2018 в 00:19
source

1 answer

0

Try replacing the double quotes " by single quotes ' and running away within the fwrite function:

<?php
$fp = fopen("users/${dirname}/validar.php", "w") or die("Error al intentar abrir el archivo!");
fwrite($fp, '<?php

$miuser = \'ChristianHz\';
$mipass = \'seraz90\';

if(isset($_POST[\'login\'])) {
    $usuario = $_POST[\'usuario\'];
    $pass = $_POST[\'password\'];
    if ($usuario == $miuser and $pass == $mipass ) {
        if (isset($_POST[\'remember\'])) {
            setcookie(\'usuario\', $usuario, time()+60*60*7);
            setcookie(\'passowrd\', $pass, time()+60*60*7);
        } 
        session_start();
        $_SESSION[\'usuario\'] = $usuario;
        header(\'location:panel\');
    } else {
        echo \'<p>usuario o clave son incorrectos</p>\';
    }
} else {
    header(\'location: login\');
}

?>');
fclose($fp);
?>

If you use double quotes, the content will be evaluated before writing to the file. For example:

<?php
$nombre = "Juan";
$variable1 = "Mi nombre es $nombre";
$variable2 = 'Mi nombre es $nombre';

echo $variable1; //Imprime: Mi nombre es Juan
echo $variable2; //Imprime: Mi nombre es $nombre
?>
    
answered by 31.12.2018 / 01:30
source