Hi,
Your script does seem a bit confused, so I will give you some ideas on how I would improve it. The following is a list of what I think the problems are, and possible solutions.
- Your script does too much stuff for one script.
If I am reading it correctly, your script does the following:
Allows the users to browse products and add them to their cart
Provides the admin user with the abillity to log in
Allows the admin user the ability to edit stock
All this functionality is to much to contain within one file - it makes the file hard to read and a nightmare to maintain... If your site grows to include more functionality in the future the script could grow.... imagine trying to maintain a 2000 line script....
The soultion: put each seperate bit of functionality into a seperate script. You can make use of includes to include multiple files at runtime (see http://uk.php.net/manual/en/function.require-once.php for details on how to include files).
I would have you site work like this:
The customers use www.mysite.com/index.php - I suppose this would default to the products page of some sort.
The admin user would use www.mysite.com/admin/index.php this would default to the admin log in page unless the admin was already logged in.
The index.php file would just be a controller to run other scripts, and would include scripts as needed. For example the login page would be www.mysite.com/admin/index.php?c=login
The index.php script would recieve a get varibale: $_GET['c'] which would contain 'login', based on this you know to do a require_once on the login script to run it.
- You seem to try to create the stock table each time the script runs - why?
Soloution: just create it once (say using phpmyadmin).
- You use extract($POST). This is bad because say you have a post variable called quantity. After using extract you will just reference it as $quantity instead of $POST['quantity']. This means when you come to modify your script a few months after first writing it, you'll be thinking 'where the hell does $quantity come from!?!'.
Solution: take the time to type out $_POST['quantity'] each time. That way when you see it in the code, you will always know that it was posted by the user from a post form.
- Using session_register
Yeah this one, when coupled with using extract($_POST) has caused a massive security flaw in your script. To demonstrate this, what would happen is I, on my pc create an html post form that submits to your sites script, and contains a form field called owner_confirmed and a value of 1.... I think I am right in saying that this would give me admin access - without a password.
Solution: use $SESSION super global array instead of session register. You still need session_start(), but now access session variables using the $SESSION array. You could then set owner_confirmed like this in your script: $SESSION['owner_confirmed'] = 1, then refer to it as $SESSION['owner_confirmed']. This means it is both more secure and more readable as you know the value you are reading has come from the session.
Let me know how you get on
Robin