Answer the question
In order to leave comments, you need to log in
PHP why does the session get hit when the page is refreshed?
Good afternoon,
I'm making a personal account in PHP using a session
after authorizing the login personal account, the
problem is that
when the page is refreshed, the session is lost
, checked like this
if (isset($_SESSION['logged_user'])){
echo 'Session exists';
}else echo 'Session does not exist';
when I log in, there is a session after refreshing the page, the session is reset
вот код login.php:
<?php
require 'db.php';
session_start();
$data=$_POST;
if(isset($data['do_login']))
{
$errors = array();
$user=R::findOne('user','login=?', array($data['login']));
if($user)
{
// логин существует
if (password_verify( $data['password'], $user->password))
{
echo "Пользователь:" .$user->login;
echo '<div style="color:green;"> переход в систему... </div><hr>';
$_SESSION['logged_user'] = $user->login;
header ( 'refresh:5; url=/kab.php');
}else
{
$errors[] = 'Неверно введен пароль ';
}
} else
{
$errors[] = 'Пользователь с таким не найден';
}
if ( ! empty($errors))
{
echo '<div style="color:red;">'.array_shift($errors).'</div><hr>';
}
}
echo '
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="admin" />
<link href="style.css" rel="stylesheet" type="text/css">
<title>Информационная система System Press </title>
</head>
<body>
<div id="contaner">
<a href="signup.php">Регистрация</a>
<div style="border-radius:10px;width:200px; margin:10px; background-color:#33b0a8;color:#fff;">
<form action="login.php" method="post">
<p>
<p><strong> Ваш логин: </strong></p>
<input type="text" name="login" style=" border-radius:10px; margin:5px; color:green;bordercolor:#0000:" >
</p>
<p>
<p><strong> Ваш пароль: </strong></p>
<input style="border-radius:10px; margin:5px" type="password" name="password">
</p>
<button style="border-radius:10px; margin:20px; background-color:yellow" type="submit" name="do_login">Войти в систему </button>
</div>
</div>
<div style="clear: both;"></div>
<div id="empty"></div>
</div>
<div id="footer">
<p style="text-align-first: 5px; padding-top: 5px;">System Press 2018©</p>
</div>
</body>
</html>';
?>
--- Добавлено ---
вот код kab.php:
<?php
include 'db.php';
session_start();
if (isset($_SESSION['logged_user'])){
echo 'Сессия существует';
}else echo 'Сессия несуществует';
$data=$_POST;
if (isset($data['do_signup']))
{
$error =array();
if (trim($data['name'])=='')
{
$error[]='<p style="color:green;baqckground:#0000;">заполните поле Имя</p>';
}
$dat = R::dispense('dat');
$dat->name=$data['name'];
$dat->telephone=$data['telephone'];
$dat->family=$data['family'];
$dat->email=$data['email'];
R::store($dat);
if (isset($_SESSION['logged_user'])){
echo 'Сессия существует';
}else echo 'Сессия несуществует';
}
echo
'<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="admin" />
<link href="style.css" rel="stylesheet" type="text/css">
<title>Информационная система System Press </title>
</head>
<body>
<div id="contaner">';
echo 'Пользователь:' . $user = $_SESSION['logged_user'];
echo ' <A href="logout.php">Выход</a>';
$_SESSION['logged_user'] = $user->login;
echo '<div id="header">
<h1 style="margin-top: 10px ;" >Информационная система System Press</h1>
</div>
<div id="content">
<hr>';
echo '
<div style="border-radius:10px;width:200px; padding:10px; margin:10px; background-color:#33b0a8;color:#fff;">
<form action="kab.php" method="post">
<p>
<p><strong> Имя: </strong></p>
<input type="text" name="name" style="color:green;bordercolor:#0000:" >
</p>
<p>
<p><strong> Фамилия: </strong></p>
<input type="text" name="family" style="color:green;bordercolor:#0000:">
</p>
<p>
<p><strong> Телефон: </strong></p>
<input type="text" name="telephone" style="color:green;bordercolor:#0000:">
</p>
<p>
<p><strong> Ваш email: </strong></p>
<input type="email" name="email" style="color:green;bordercolor:#0000:">
</p>
<button type="submit" style="" name="do_signup">дабавить в базу </button>
</div>
';
echo '</div>
<div style="clear: both;"></div>
<div id="empty"></div>
</div>
<div id="footer">
<p style="text-align-first: 5px; padding-top: 5px;">System Press 2018©</p>
</div>
</body>
</html>';
?>
Answer the question
In order to leave comments, you need to log in
At you in check the check on value is used, and it can be and null.
<?php
session_start();
echo '<pre>';
if (isset($_SESSION['logged_user'])) {
echo 'Сессия существует для isset'.PHP_EOL;
} elseif (array_key_exists('logged_user', $_SESSION)) {
echo 'Сессия существует для array_key_exists'.PHP_EOL;
} else {
$_SESSION['logged_user'] = null;
echo 'Сессия не существует'.PHP_EOL;
}
echo 'Session Id:'.session_id().'</pre>';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question