User control using mysql query in PHP

Posted on 2010-11-19
Last Modified: 2012-05-10
I have 200 products, each user only has control of certain products that they can edit.  I have 3 tables in my database.

I have a table for "users" that has the user_id (auto incrementing number), username, password and email.

Next table is "users_products" that has their up_user_id number and then up_product_id number.

Then I have products which has product_id, name, price.

I store their user id in a session.

Through the URL is how I pass my variables to each page.  So for instance

I want to make it so on edit_product.php that they can only edit product_ids that are listed with their user_id in "users_products".  Or else they can simply manipulate the URL and change the id to some product that they aren't allowed to edit.

This is the query I use on the "view_products.php" page so they only see their products

SELECT product_id, name, price FROM users_products, products WHERE product_id=up_product_id AND up_user_id='20' GROUP BY product_id

Question by:kevandju
  • 5
  • 3
  • 2
  • +2

Expert Comment

ID: 34176631
So what is exactly your question? Use a similar query in edit_product.php:
SELECT products.* FROM users_products, products
WHERE product_id='$productId'
AND product_id=up_product_id
AND up_user_id='$userId'

Author Comment

ID: 34176636
My question is that I need to validate that the user is allowed to edit the product id or else take them back to view_products.php.  I don't want someone to edit the URL and start editing products that aren't authorized to edit.

Expert Comment

ID: 34176684

Use this query to check if they can edit:

SELECT product_id, name, price FROM users_products, products WHERE product_id=up_product_id AND up_user_id='20' AND product_id IN (SELECT users_products WHERE up_user_id='20') GROUP BY product_id

Expert Comment

ID: 34176696
Sorry, this should be the correct one:

SELECT product_id, name, price FROM users_products, products WHERE product_id=up_product_id AND up_user_id='20' AND product_id IN (SELECT up_product_id FROM users_products WHERE up_user_id='20') GROUP BY product_id
LVL 108

Expert Comment

by:Ray Paseur
ID: 34177232
Do your users sign in with password authentication?  If so, you've got the user id.  And with the user id you can make a SELECT against the users_products table.  If the thing they want to edit matches, you're OK.  If there are no rows in the results set, they cannot make the edit.  I'm guessing that the id= string in the URL GET argument is the id of the products, right?

Author Comment

ID: 34177714
Yes they sign in with password identification and the user_id is stored in their session so I have access to that.

I"m thinking I need to run a query to see if they user_id is allowed to see that product_id and if results of the query returns 0 rows then I will just redirect them back to view_products.php.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Expert Comment

ID: 34177737
I suggest generating a fairly long random string and setting this as a session cookie whenever they log in. Compare the string from the cookie with the string in temporary session storage on the server, and bam your user is authenticated. Then it will be impractical for users to guess this long string. If you can't use cookies, compare ip address with $ip = getenv("REMOTE_ADDR") ;


Accepted Solution

kevandju earned 0 total points
ID: 34177857
Here is code that I put on my pages that works.  Store product id as $p and store user id as $id then check to see if user is an admin, if they are not then I check to if there is a row in users_products with that user id and product id, if not I redirect them back to the view_products.php page.  

if ($_SESSION['user_type'] != "admin") {
            $id = $_SESSION['user_id'];
            $query2="SELECT * FROM db.users_products WHERE up_user_id='$id' AND up_user_products='$p'";
            $result2=@mysql_query ($query2);
            $num2 = mysql_num_rows($result2);
            if ($num2 == 0) {
                  $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
                  if ((substr($url, -1) =='/') OR (substr($url, -1) == '\\')) {
                        $url = substr ($url, 0, -1); //chop off the slash

                  $url .= '/view_products.php';
                  header("Location: $url");
LVL 108

Expert Comment

by:Ray Paseur
ID: 34178040
Looking at this comment from ID:34177714
"... if results of the query returns 0 rows then I will just redirect them back to view_products.php."

Here is how I might design it.  For every product that is displayed on the view_products page check the users_products table.  If they are allowed to update the product, you can add a link that says, "Update this" and you can omit that link from the other product displays.  You still have to perform server-side validation of any update requests, but your site will make sense to all the visitors because they will only see "Update this" links on the products they control.

A little bit of clever query design can make this very efficient.  Perhaps one query gets all the rows for all the products and a second query gets all the rows that the client controls.  Then as you produce the HTML, you can test with in_array() to see if the product should get the "Update this" link.

Author Comment

ID: 34178075
I already do this. The view products page only shows their products. I was trying to avoid people manipulating the URL to edit products that weren't theirs.
LVL 108

Expert Comment

by:Ray Paseur
ID: 34178228
Then just check the users_products table whenever an update request is received and only proceed if there is a match between user and product.  It should work great.

Author Closing Comment

ID: 34203814
Was able to come up with the query and solution on my own for this problem.

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now