How to Secure and Handling variables
I've been working with a PHP site and below is the code for my index page.. how can i check the data first before going to POST? can i do it with a javascript? if the textboxes are blank it will not POST and just give a message.. and How can make it better and more secured? anyone who could help me?
Ayn Interactive
<?php
session_start();
include("aynconfig.php");
if (isset($_GET['action'])) {
$action = $_GET['action'];}
else {
$action = "Home";
}
switch($action){
case "Log":
include("Header.php");
if ($_POST['uname']==""){
echo "Please indicate a username";
echo "";}
else{
$connect = mysql_Connect($hostname,$username,$password) or die ("Could not connect to mysql server");
$uname = $_POST['uname'];
$passw = $_POST['passw'];
$dbname=mysql_select_db($database);
$query = "SELECT * FROM users where uname=\"$uname\" and passw=\"$passw\"";
$result = mysql_query($query) or die ("Query failed: " . mysql_error());
if (mysql_num_rows($result) > 0) {
echo "You are now Logged in";
$_session['name'] = $uname;
echo $_session['name'];
}
else {
echo "You are not a valid user!";
unset($uname);
unset($passw);
//unset($admin);
echo "";
echo "";
}
mysql_free_result($result);
mysql_close($connect);
}
break;
case "Home":
if (isset($logged)) {
}
else{
include("Header.php");
echo "
";
echo "Username
";
echo "Password
";
echo " ";
}
break;
case "Comments":
break;
}
bornok15
Junior Poster in Training
91 posts since Feb 2008
Reputation Points: 13
Solved Threads: 2
You can use Javascript to check if the text boxes actually have data, OR you can use PHP, OR you could use both.
My #1 rule when dealing with user input is that I always always always check that it is valid before I start doing anything with it.
So what do when I want to check if a form has passed data I do the following:
if(!isset($_POST['uname']) || $_POST['uname'] == '' || !isset($_POST['pword']) || $_POST['pword'] == ''){
// do what I need to do to return to the form
}
JRSofty
Junior Poster in Training
69 posts since Dec 2007
Reputation Points: 16
Solved Threads: 10
To do a simple form validation its useful to use javascript:
.
.
// javascript function:
function checkContactForm(form, errorName)
{
er = true;
if(form.txtNume.value == "")
{
alert(errorName);
er = false;
}
return er;
}
it's just a part of the code ....
silviuks
Junior Poster in Training
96 posts since Apr 2006
Reputation Points: 10
Solved Threads: 15
Thanks guys.. are my codes considered as server side scripting? how can i make it like a client side? silviuks, i tried the code i don't seem to understand how it works.. can you give me some other examples just small ones like this.. Thanks..
bornok15
Junior Poster in Training
91 posts since Feb 2008
Reputation Points: 13
Solved Threads: 2
I think i can do the server side validation but im having problems with the client side using javascript. can you give a simple way to do it? I don't know how to prevent POST if the variables are null..
Will this work?
bornok15
Junior Poster in Training
91 posts since Feb 2008
Reputation Points: 13
Solved Threads: 2
Thanks.. ill try this one now..
bornok15
Junior Poster in Training
91 posts since Feb 2008
Reputation Points: 13
Solved Threads: 2
it doesn't stop the POST method.. on the onclick event with the returning function, how does it stop the method? is there another way?
this the code
function declaration:
Ayn Interactive 2008
<?php
session_start();
include("aynconfig.php");
form:
echo "
";
echo "Username
";
echo "Password
";
echo " ";
both of them are in the same file under index.php
bornok15
Junior Poster in Training
91 posts since Feb 2008
Reputation Points: 13
Solved Threads: 2