1
On my site I have a profile page, when the user logs in he is redirected to the page perfil.php
, this login is performed on the page entrar.php
.
So on the page enter I have:
enter php.
<?php include "controller/functions.php";?>
<?php include "controller/db_ss_user_entrar.php";?>
<?php include "view/doctype.php";?>
<html>
<?php include "view/head.php";?>
<body>
<?php include "view/header.php";?>
<div id="content">
<?php include "view/wrap_entrar.php";?>
</div>
<?php include "view/footer.php";?>
<script src=js/entrar.js></script>
</body>
</html>
The relevant file for login is db_ss_user_entrar.php
. The section related to session is indicated in the code below:
db_ss_user_log in.php
<?php
include 'db_conect.php';
$place_email = "Insira um email";
$place_senha = "Insira uma senha";*/
$email_place = "Insira um email";
$email_err = "";
$senha_place = "Insira uma senha";
$senha_err = "";
$error = 0;
if($_SERVER["REQUEST_METHOD"] === "POST")
{ $value_email = mysqli_real_escape_string($con, preg_replace('/\s+/', '', $_POST['email']));
if(empty($_POST["email"]) ||
$value_email === "" ||
$value_email === "Email inválido" ||
!preg_match("/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i",$value_email))
{ $value_email = "Email inválido";
$error++;
}else
{ $value_email = mysqli_real_escape_string($con, preg_replace('/\s+/', '', $_POST['email']));
}
$value_senha = mysqli_real_escape_string($con, $_POST['senha']);
if(empty($_POST["senha"]) ||
!preg_match("/^\S*$/",$value_senha))
{ $place_senha = "Senha inválida";
$error++;
}else
{ $value_senha = mysqli_real_escape_string($con, $_POST['senha']);
}
if($error !== 0)
{ return false;
}
$sql = "SELECT input_nome,input_email,input_tel,input_senha,user_id,img_perfil FROM form_user WHERE input_email = '$value_email' AND input_senha = '$value_senha'";
$result = $con->query($sql);
$linha = $result->num_rows;
if($linha != 1)
{ $error++;
$value_email = "Email inválido";
$value_senha = "";
$place_senha = "Senha inválida";
$con->close();
return false;
}else //<-----------------Aqui começa o trecho relativo a sessão
{ $row = $result->fetch_object();
session_start();
$_SESSION['ss_nome'] = $row->input_nome;
$_SESSION['ss_email'] = $row->input_email;
$_SESSION['ss_id_user'] = $row->user_id;
$_SESSION['ss_tel'] = $row->input_tel;
$_SESSION['ss_s_user'] = $row->input_senha;
if(($row->img_perfil) === "")
{ $_SESSION['ss_img_perfil'] = "img/icon_perfil_bluegrey.svg";
}else
{ $_SESSION['ss_img_perfil'] = $row->img_perfil;
}
header('location:perfil');
}
}
?>
So far all goes well the user is redirected to the profile page and giving a var_dump($_SESSION)
, i see that session has the data I need.
The problem occurs on the profile page that has a link that allows the user to edit the profile:
<a href="editar-perfil" class="btn_fImob">Editar Perfil</a>
Then on the page editar-perfil.php
I have the following code:
<?php include "controller/functions.php";?>
<?php include "controller/security.php";?>
<?php include "controller/db_select_user.php";?>
<?php include "view/doctype.php";?>
<html>
<?php include "view/head.php";?>
<body>
<?php include "view/header.php";?>
<div id="content">
<div id="content_perfil">
<?php include 'view/wrap_perfil_edit.php';?>
</div>
</div>
<?php include "view/footer.php";?>
</body>
</html>
The problem is that when redirected from the page perfil.php
for editar-perfil.php
, the session variable gets null and automatically redirects to enter, the code that does this is from the file security.php
:
security.php
<?php
session_start();
if(empty($_SESSION['ss_email']))
{ session_destroy();
unset ($_SESSION['ss_email']);
header('location:entrar');
}
?>
That is the session data is not being passed to the page editar-perfil.php
, and I don’t understand why.
Could you check the page
editar-perfil.php
before include a Session exists with var_dump– Sam
@dvd thanks for your attention, come zaio.
– MagicHat
Actually, it wouldn’t matter, because session_start(); it’s inside include ;/
– Sam
Maybe doing the var_dump after the includes
– Sam
@dvd even I taking out the other includes (commenting on the lines) and putting on top
session_start()
in the archiveeditar-perfil
If it’s empty... it’s a blow– MagicHat
Complicated... maybe a more "raw" test... backs up the page
editar-perfil.php
and empty it all and just leave session_start and var_dump to see if it’s going empty... if it’s going full, it’s some trouble code– Sam
With my head full it was working, I can’t imagine what it might be
– MagicHat
Have some framework working behind or it’s all neat ?
– Isac
Thanks for the attention @Isac is pure...
– MagicHat
I would start by doing some sanity tests. Testing a
var_dump
from the top session and browse multiple pages to see if it stays on some/all/none pages. Then confirm if thewarnings
are active, because if you have inactive you may not be seeing aheaders already sent
that will make the session not work. That would be the case if you were writing any strange characters beforesession_start
or directly or through the inclusion of another file.– Isac