I've created a script for employees to go in and edit their contact information. The script is accessed after logging in with the proper authentication and establishing a session. However, there seems to be a security loop-hole. After logging in, if the employee knew enough they could easily just change the URL and edit other employees' contact information. How can I alter my code so that the authentication is good only for the URL authenticated for?
Here is what I'm using for the login:
<?
session_start();
if($action=="logout")
{
session_destroy();
$message = "You have successfully logged off.";
}
if(isset($submit))
{
$SQL = "SELECT username, password, id FROM employees WHERE username = '$username' AND password = '$password'";
mysql_connect("localhost","xxx","xxx");
$server = "localhost";
$user = "xxx"; $pass = "xxx"; $database = "xxx";
$connection = mysql_connect($server, $user, $pass);
mysql_select_db("cu_editx");
$result = mysql_query($SQL);
if(mysql_num_rows($result)>0)
{
$row = mysql_fetch_array($result);
$myname = $row["username"];
session_register("myname");
header("location: edit.php?id=$row[id]");
}
else
{
$action = "fail";
$message = "Your USERNAME/PASSWORD is incorrect.";
}
mysql_close($connection);
}
?>
<div align="center">
<p> </p>
<form name="login" action=index.php method=post>
<table width="100%" border="0" cellspacing="0" height="203">
<tr align="center">
<td height="240"> <br>
<br>
<? echo $message; ?>
<table width="40%" border="1" cellspacing="0" height="80" bordercolor="#0033CC">
<tr>
<td height="81">Username</td>
<td height="81">
<input type="text" name="username">
</td>
</tr>
<tr>
<td height="81">Password</td>
<td height="81">
<input type="password" name="password">
</td>
</tr>
</table>
<p>
<input type="submit" name="submit" value="Submit">
<br>
<br>
</p>
</td>
</tr>
</table>
</form>
<p> </p>
</div>
Here is what I'm using for the editor:
<?
session_start();
if(!isset($myname))
{
header("location:index.php");
}
?>
<?
session_start();
$id = $_GET["id"];
$server = "xxx";
$user = "xxx";
$pass = "xxx";
$database = "xxx";
$connection = mysql_connect($server, $user, $pass);
mysql_select_db($database, $connection);
if (isset($submit))
{
$sql = "update employees
set
first = '$first',
last = '$last',
department = '$department',
title = '$title',
bio = '$bio',
extension = '$extension',
email = '$email',
bday = '$bday',
anniversary = '$anniversary',
homephone = '$homephone',
cellphone = '$cellphone',
pager = '$pager',
password = '$password'
WHERE id = $id";
$result = mysql_query($sql, $connection);
}
$sql = "SELECT * FROM employees WHERE id = '$id'";
$result = mysql_query($sql, $connection);
while ($row = mysql_fetch_array($result)) {MY_HTML_EDITING_FORM_HERE}
mysql_close($connection);
?>