ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I think you need to discover what happens when. Slow down a bit, you're moving too fast for yourself. First answer this: what happens when you type in a URL or click a link? You must first understand the basic principals of the internet. At least understand this:
JAVASCRIPT HAPPENS ON THE CLIENT
PHP HAPPENS ON THE SERVER
I use client side to speed up validation and minimize server load. The problem is that the SQL is executed when the page is loaded into the browser even before a user can see the form.
Did anyone else notice the blindingly obvious problem with the original script?
There was no check to see if the form had been resubmitted before doing the database connection. There is no need to seperate the two into seperate files if there is a check, say for the submit button being clicked.
$Array["user"] = trim ($user);
$Array["email"] = trim ($email);
$Array["PASSWORD"] = trim ($PASSWORD);
//connect to the database:
if (!($Link = @ mysql_connect($Host,
$Username,
$adminPassword)))
die("Could not connect");
//execute the query:
$Query = "INSERT into $TableName values ('0','$Array[user]','$Array[email]','$Array[PASSWORD]','1')";
if(mysql_db_query ($DBName, $Query, $Link)){
print ("Your user account was successfully created!<br>\n");
} else{
print("There was an error<br>\n");
}
mysql_close ($Link);
}
?>
The simple addition of the isset method will check if there is a variable called submit_form (the name of the button) set and if so it assumes the form has been filled in and does the database part.
I mean no offence whan I say this but if you don't know how to do this may I suggest getting a good PHP tutorial book like SAMS PHP in 24hrs, this covers forms and other useful PHP methods. I taught myself the basics using this book.
Andy@DP, thanks a lot for your lesson.
I do have a few good books. The problem is not lack of information, but management of information
Thank you very much!
N.B. Note to the (unexperienced) coders who might copy and reuse this code:
THIS CODE IS NOT SECURE!!! If you put it on the Web, you will have to add a few security bells
Testing for the submit button like that is quite sloppy... I would suggest testing for several things and validating data beforehand. This was only a short lesson on self-submitting PHP pages NOT a definative solution. As linuxfond said this is not secure and open to abuse.
Edit: OK I'm going to add code examples to explain. The $submit_form variable is created by the submission of the form. It relates to the submit button.
The script has NO way of knowing if it was POST or GET
Any person can add ?submit_form=foo to the end of the URL and hey presto the isset ($submit_form) now returns true! and you end up with a totally false submission.
You should check first if each entry is filled in again with PHP and that the data came to the script the correct way (using the $HTTP_POST_VARS[] array for example). Also you should check WHERE the request came from to make sure someone has not knocked up a little script of their own. Make sure the referer id the script itself and not a remote script...
There are lots of security articles for PHP. Read them for a better understanding. This is only a TASTER of things that can go wrong
I totally agree that the script is very welcoming for hackers, but, the form does have a method :
method='post'
Is that not enough to tell whether it is GET or POST?
the method only tells the browser how to send the information. GET is in the URL and POST in the HTTP headers. Using php variables like $submit_form which are created by GET/POST do not distinguish between the two and thats why I suggested using $HTTP_POST_VARS and $HTTP_GET_VARS. At least that way you are sure which way the data arrived at the script!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.