Answer the question
In order to leave comments, you need to log in
Why is php code executed in js code (session)?
When the site is launched, the login is equal to guest, if you enter user1 and log in, everything seems to be ok, but if you restart the site 2 times, then the code will be executed, as if you clicked on exit, although you didn’t click anywhere and the login will become guest1. Why is php code being executed when it shouldn't be executing
HTML
<?php
include_once "php/session.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<input type="text" id="login">
<button id="btn" onclick="singin();">Sign In</button>
<button id="btn" onclick="singout();">Sign Out</button>
<p id="login_enter"></p>
<script>
var str = '<?php echo $_SESSION['login'];?>';
document.getElementById("login_enter").innerHTML = str;
function singin () {
var login = document.getElementById('login').value;
jQuery.ajax({
url: "php/server.php",
type: "POST",
dataType: "html",
data: "login="+login,
success: function(response) {
document.getElementById("login_enter").innerHTML = response;
},
error: function(response) {
}
});
}
function singout () {
<?php $_SESSION['login'] = "guest1"?>
str = '<?php echo $_SESSION['login'];?>';
document.getElementById("login_enter").innerHTML = str;
console.log("exit");
}
</script>
</body>
</html>
<?php
require "session.php";
$login = "";
$FORM['login'] = "";
if (isset($_POST['login'])) $FORM['login'] = htmlspecialchars($_POST['login']);
$login = $FORM['login'];
if ($login == "user1") {
$_SESSION['login'] = "user1";
echo $_SESSION['login'];
} else {
echo "Такого пользователя нету";
}
?>
<?php
session_start();
if (!isset($_SESSION['login'])) {
$_SESSION['login'] = "guest";
}
?>
Answer the question
In order to leave comments, you need to log in
$_SESSION['login'] = "guest1"
You are overwriting the session
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question