Answer the question
In order to leave comments, you need to log in
How to work with sessions in php?
Good afternoon,
I am writing a small project in pure php, I have not tried it before - I used slim, laravel, bitrix, all sorts of forms. And somehow I did not think about the work of the sessions.
Now I'm doing crutch authorization (just to be). And I ran into a problem.
I understand that in order to work with sessions, you need to register session_start ()? And where to do it? I paste it before
. So it turns out:
<?if(!$_SESSION)session_start();?>
<!DOCTYPE html>
<html>
<head>
Answer the question
In order to leave comments, you need to log in
The problem was pretty obvious. It consisted in the output of errors and unnecessary checking.
Used error_reporting(E_ERROR | E_PARSE);
To avoid warnings that the variable is not defined.
And then, after reading a little more docks on the session, I decided that the check, in general, was superfluous. Left just session_start(); Because it not only starts a new session, but also continues the existing one. Therefore, in the additional check - there is no point.
When the session starts, a cookie (session id) is set, which is passed in the response header. Therefore, the session requires that no headers be sent. You can turn off the setting of cookies by session
In general, I do not recommend using native PHP sessions, there are a lot of rakes. And one rake has already been stepped on. There will be more.
ini_set('session.use_cookies', '0');
if (!isset($_SESSION)
session_start();
if (isset($_POST['auth'])) {
if($func->checkPassword($_POST['login'], $_POST['password'])) {
$_SESSION[name] = $_POST['login'];
header("Location: cp.php");
}
else
$msg = "Не верный логин или пароль";
}
if($func->checkPassword($_POST['login'], $_POST['password'])) {
if(!isset($_SESSION['name']) {
//Любые манипуляции если он не залогинен
//например кинем на login.php
header('Location: login.php')
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question