Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

User control using mysql query in PHP

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 http://domain.com/edit_product.php?id=7

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

0
kevandju
Asked:
kevandju
  • 5
  • 3
  • 2
  • +2
1 Solution
 
smantscheffCommented:
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'
0
 
kevandjuAuthor Commented:
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.
0
 
armchangCommented:
Hi,

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
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
armchangCommented:
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
0
 
Ray PaseurCommented:
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?
0
 
kevandjuAuthor Commented:
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.
0
 
DFPercushCommented:
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") ;

http://www.w3schools.com/php/func_http_setcookie.asp

0
 
kevandjuAuthor Commented:
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");
                  exit();
            }
      }
0
 
Ray PaseurCommented:
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.
0
 
kevandjuAuthor Commented:
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.
0
 
Ray PaseurCommented:
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.
0
 
kevandjuAuthor Commented:
Was able to come up with the query and solution on my own for this problem.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now