I have urls being generated with a simple integer on the site that allows the user to see past orders.
http://site.com/order_id/145323
The problem is that it is very easy to just change that number and then view other peoples orders. I am wondering what would be an easy way to encode this number in the url and then have the php decode it so it can run the controller? It does not have to be super secure, juse something that makes it not so easy for someone to just change the number to see the data. I tried doing something like :
order_id/<?php echo base64_encode($theOrder); ?>
and then in the controller action I tried :
if($orderId = (int) $this->getRequest()->getParam('order_id') && Mage::getSingleton('customer/session')->isLoggedIn()){
$orderId = (int) $this->getRequest()->getParam('order_id');
$orderId = base64_decode($orderId);
$order = Mage::getModel('sales/order')->load($orderId);
Mage::register('current_order', $order);
$this->loadLayout();
$this->renderLayout();
}
but this doesnt work. What would be a good way to obscure this parameter in the url ?
You're doing it wrong. Instead of trying to obscure the id
you just have to make sure (check on the backend) that the id
belongs to the user.
And if the id doesn't belong to that user return a status code of either 403 Forbidden
or simply 404 Not Found
.
Surely if its about people and orders, you will have a users table or datastore, when the random url is parsed, check if that order number belongs to the currently logged in user, if it does, allow the data, if not, return 'unauthorised, please login'
EDIT
I'm not sure what you're system entails, but running on pure assumption, this may be useful...
I'm assuming the USER makes the order. So I'm also assuming that there is an ORDERS table, where all the orders are stored and fetched from.
In the ORDERS table, you should store the user that made the order, say
USERS table
----------
user_id, username, password, full name, dob, etc
ORDERS table
----------
order_id, order_type, quantity, date_ordered, user_id
therefore you can use an SQL query to get the user associated with the order.
Something like SELECT username FROM users JOIN orders ON users.user_id = orders.uder_id
then check the returned username against the currently logged in user.
I'm sorry if i'm insulting your intelligence, but i'm not sure exactly what you're after!
As others have said, the best way to fix this is to amend the way you're accessing the data, and put in a check that the order number from the request is linked to the user that's trying to access it.
But if you need a very quick fix, you can do a check to see if the request's HTTP_REFERER is set. If it's set, and it points back to a page on your site, then you can be reasonably sure that this is a legitimate request. If a user has modified the URL by amending the order number, the HTTP_REFERER will be empty.
It's not a perfect solution, but it'll definitely stop people being able to browse orders they shouldn't.