jeudi 5 février 2015

PHP Session variables disappearing on new page?


First of all, I'm going to show my project directory in WampServer to understand my question. app is the application in PHP with Slim Framework, and maybe in the future it becomes a web service (no yet). The rest of files outside this folder are the web for the user:


Project directory


I'm working in PHP with session variables and Slim Framework. On page login.php I created two session variables. This file is not in Slim Framework app folder I'm doing.


This is the file login.php:



<?php
session_start();

if (!empty($_SESSION) && array_key_exists("sesionIniciada", $_SESSION)) {
if ($_SESSION["sesionIniciada"] === true) {
header('Location: /index.php');
} else {
session_destroy();
$_SESSION["LLAVE_ACCESO_WEB"] = bin2hex(openssl_random_pseudo_bytes(32));
$_SESSION["LLAVE_PUBLICA"] = bin2hex(openssl_random_pseudo_bytes(32));
}
} else {
session_destroy();
$_SESSION["LLAVE_ACCESO_WEB"] = bin2hex(openssl_random_pseudo_bytes(32));
$_SESSION["LLAVE_PUBLICA"] = bin2hex(openssl_random_pseudo_bytes(32));
}

?>


When the variable $_SESSION["sesionIniciada"] doesn't exists, or is false, I destroy the session and create two new variables. No problems there.


In the same page, I have a login form in HTML and when I press the submit button, I call a jquery AJAX function to log in.


This is the function in Javascript:



var url = "./app/v1/acceso/web";
var data = JSON.stringify({
usuario: $.trim($("#usuario").val()),
clave: $.trim($("#clave").val())
});

$.ajax({
type: "POST",
url: url,
data: data,
processData: false,
headers: {
'S-Publica': $.trim($("#llave").val()),
'S-Hash': encriptacion(data, $.trim($("#llave").val())),
'Content-Type': 'application/json'
},
success: function (response){
console.log(response);
},
error: function (e){
console.log(e);
}
});


I don't have problems with this function, because the request was correct.


The problem is in the PHP file I am calling through AJAX: var url = "./app/v1/acceso/web". On the project directory image is app/v1/index.php with a router to the link /acceso/web. Inthis file I'm trying to get the $_SESSION variables I created in login.php, but They don't exist! It's very strange because all the project is in the same server (and I suppossed it doesn't have problems with the cookie ID).


This is a part of index.php:



<?php
session_start();

require '../libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim(array(
'mode' => 'debug'
));

$app->post('/acceso/web', function () {
var_dump($_SESSION); //doesn't show the variables session created in login.php
});


I don't have idea what is wrong! I'm thinking is the .htaccess file used for Slim, but I don't sure about it! In any case, I publish the .htaccess file:



RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]


What's the problem? Thanks!





Aucun commentaire:

Enregistrer un commentaire