If you mean that some of your pages ask user login, check from google javascript password,How to create login file in this free web hosting site?
, [HTML] or [PHP] tags, as appropriate.
[quote="netcalle64, post: 769959"]this doesn't work directly but gives good idea how to[/QUOTE]
That code gives a bad idea how to.
[list]
[*]The database should handle data operations (such as selecting data with given properties from a dataset) and programs the rest. As it is, the code wastes resources by fetching too many rows and [URL="http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-use-select"]columns[/URL] from the database. A simple [URL="http://en.wikipedia.org/wiki/Where_(SQL)"][FONT="Courier New"]WHERE[/FONT][/URL] clause would improve it dramatically.
[*]There are also security problems. MD5 is broken, and some of the passwords can be cracked with [URL="http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html"]rainbow tables[/URL].
[*]Nondescriptive variable names make the code less [URL="http://en.wikipedia.org/wiki/Readability#Readability_in_computer_programming"]readable[/URL].
[*]Then there are the more advanced issues, such as the [URL="http://en.wikipedia.org/wiki/Coupling_(computer_science)"]coupling[/URL] between authentication and display.
[/list]
<form method="post" action="verifylogin.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="100"><p id="blue">User Name: </p></td>
<td><input name="txtUser" type="text" id="txtUser"></td>
</tr>
<tr>
<td width="100"><p id="blue">Password: </p></td>
<td><input name="txtPass" type="password" id="txtPass"></td>
</tr>
<tr>
<td width="100"> </td>
<td><input type="submit" method = "post" name="btnLogin" value="Login"><br></td>
</tr>
<tr><td> </td></tr>
</table>
</form>
<?php
// username and password sent from form
$user=$_POST['txtUser'];
//$pass=sha1($_POST['txtPass']);
// To protect MySQL injection (more detail about MySQL injection)
$user = stripslashes($txtUser);
$pass = stripslashes($txtPass);
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
include 'databaseconfiguration.php';
function db_connectme() {
global $dbhost;
global $dbuser;
global $dbpass;
global $dbname;
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!(mysql_select_db($dbname,$connection))) {
echo "Could not connect to the database";
}
return $connection;
}
db_connectme();
$query1="SELECT pepper_value FROM users_table WHERE user_name = '$user'";
$result1=mysql_query($query1);
while($row = mysql_fetch_array($result1))
{
$pepper_pass = $row['pepper_value'];
}
$pass = sha1($pepper_pass . $_POST['txtPass']);
$query="SELECT user_name, user_pass FROM users_table WHERE user_name = '$user' and user_pass = '$pass'";
$result=mysql_query($query);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $user and $pass, table row must be 1 row
if($count==1){
// Register $user, $pass and redirect to file "logged_in.php"
session_start();
session_register("user");
session_register("pass");
$_SESSION['db_is_logged_in'] = true;
header("location: index.php");
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
mysql_connect("localhost", $user , $pass);
mysql_select_db("databasename");
$query = "SELECT user_name FROM users_table WHERE user_name='$user' AND user_pass='$pass'";
$result = mysql_query($query);
if ($result != ' ')
{
//
}
else {
echo "Wrong Username or Password. Please verify and log in again!";
}
?>
<?php
if(isset($_SESSION['user']))
{
$user = $_SESSION['user'];
$query1 = "SELECT * FROM users_table WHERE user_name = '$user'";
$result1 = mysql_query($query1) or die(mysql_error());
while($r = mysql_fetch_array($result1)){
$rights = $r[11];
$_REQUEST['defi'] = $r['user_id'];
}
$_SESSION['uid'] = $sid;
}
if (!isset($_SESSION['uid'])|| $_SESSION['db_is_logged_in'] !== true) {
// not logged in, move to login page
echo 'you are not logged in';
exit;
} else
{
echo "Welcome ".$_SESSION['user'];
}
//more code here...
?>
The two biggest would be two switch to PDO (so you can use prepared statements) and to separate authorization, user accounts, database access, and all the various concerns.Any improvement ideas are welcome!