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?
<head>
<title>Ayn Interactive</titlle>
</head>
<body>
<?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 "<script>function redirect(){window.location.replace('index.php?action=Home');}setTimeout('redirect();', 1000);</script>";}
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 "<script>alert('Invalid username')</script>";
echo "<script>function redirect(){window.location.replace('index.php?action=Home');}setTimeout('redirect();', 1000);</script>";
}
mysql_free_result($result);
mysql_close($connect);
}
break;
case "Home":
if (isset($logged)) {
}
else{
include("Header.php");
echo "<center><br><form action=index.php?action=Log method=POST>";
echo "<font face=haettenschweiler>Username <input type=text name=uname><br>";
echo "Password <input type=password name=passw></font><br><br>";
echo "<input type=submit value='Login'>  <input type=reset value='Reset'></form>";
}
break;
case "Comments":
break;
}