0
It is correct/safe to set a Session in PHP and recover this value using AJAX?
I am creating an application using transparent Pagseguro checkout and need to pass a value as Reference (Identifier). However, because it is a restricted data, I would not like to leave it in the HTML Front. Then I did it as follows (example):
In the FRONT-END
<?php
session_start();
$_SESSION['ref'] = 123;
?>
<html>
<head></head>
<body>
<button type="button" id="btnSession">Testar Sessão</button>
</body>
</html>
JQUERY SCRIPT with AJAX call
$(function(){
$("#btnSession").on("click",function(){
$.get("backend.php",
function (res){
console.log(res);
})
})
})
In the Back-End
<?php
session_start();
echo $_SESSION['ref'];
?>
This is working and it fits me perfectly. However, I would like to know if this is correct and safe for my application or if there is a better/safer/more effective way to do this with AJAX.
The purpose of this application is: I have an HTML form, I will pass its values to a JQUERY SCRIPT that will connect to PAGSEGURO and send this data via AJAX to another PHP page. This page will receive the data and validate the purchase. However, I need to inform a REFERENCE variable that is the user ID. And if I put this variable in my HTML, it risks being changed. So I thought to set the REFERENCE variable through a SESSION on the page that is HTML and then return its value on the PHP validation page. It is now clear?
I understood friend, I will read every link you shared with me, thank you. In relation to the 3rd stanza of his commentary, the " echo $_SESSION['ref'] " was just an example... in relation to the 4th stanza, the " $_SESSION['ref'] = 123; " was another example too... What I need to do is the following: I need to pass a REF variable that identifies the user. If I put this REF in the html, the user can change it. So I thought to set a Session with his REF and then return this value on the page that AJAX will access, so everything is in PHP. Got it?
– Eduardo Pereira