Reputation: 95
Let's say I have the following URL:
http://test/order?id=263&name=John
A php file handles the URL and I use $_GET to take the data from the URL and bind it to a variable:
<?php
$id = $_GET['id'];
$name = $_GET['name'];
?>
As it stands now, the user is able to change the URL and subsequently the values of the variables. I want the variables to be bound once and not to be subjected to change after. Is there any way to do that with PHP?
Upvotes: 2
Views: 990
Reputation: 418
you could actually store them in session....
1)at the very top of the page initialize the session
2) check if the value in session exists and if not create it.
at this point every further change will not be taken in consideration,
<?php
session_start();
if (!isset($_SESSION['user'])) {
$_SESSION['user'] = [
'id' => (int) $_GET['id'], //Cast the id to int
'name' =>urldecode($_GET['name']) //url decode the name
];
}
Now you have your data stored in session and you can call it using:
$_SESSION['user']['id']
$_SESSION['user']['name']
and they will never be overwritten, if you want to be updated on every call or change it if some parameter has been passed you can add some option in the condition
if (!isset($_SESSION['user']) && $_GET['updateData') == 1) {
$_SESSION['user'] = [
'id' => (int) $_GET['id'], //Cast the id to int
'name' =>urldecode($_GET['name']) //url decode the name
];
}
Upvotes: 5
Reputation: 661
<?php
start_session();
if(isset($_SESSION['name'])){
$name = $_SESSION['name'];
$id = $_SESSION['id'];
} else {
$_SESSION['id'] = $_GET['id'];
$id = $_GET['id'];
$_SESSION['name'] = $_GET['name'];
$id = $_GET['name'];
}
?>
You could try something like this.
Upvotes: 1
Reputation: 2291
Just save those variables in session or in some file until session is closed, if you have long session (login/logout). You can create array in session and keep there all these ids along with session ids. Hope that helps
Upvotes: 0