jason246day
Programmer
I am creating an administration mode for a website, and I thought everything was working great. But now I've noticed that sometimes the session won't start, and this won't allow the user to gain access to the admin mode. Here is the code I use to login. Does anyone have any idea what may be causing this.
<?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
require("header.htm");
echo "<table width=\"75%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"><tr>";
echo "<td width=\"21\"><img src=\"images/news_left.jpg\" width=\"21\" height=\"43\" alt=\"left side news title bar\"></td>";
echo "<td align=\"center\" background=\"images/news_bg.jpg\"> </td>";
echo "<td align=\"right\" width=\"24\"><img src=\"images/news_right.jpg\" width=\"24\" height=\"43\" alt=\"right side news title bar\"></td></tr></table>";
switch($action) {
default:
echo "<form name=\"form1\" method=\"post\" action=\"admin.php?action=login\">\n";
echo "<p class=\"title\">Username: <input type=\"text\" name=\"user\" size=\"20\"><br>\n";
echo "Password: <input type=\"password\" name=\"pass\" size=\"20\"><br><br>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Login\"></p></form>";
break;
case login:
$user_file = fopen("db/users.txt", "r");
$user_line = fgets($user_file);
$user_data_arr = explode("|", $user_line);
$user_name = $user_data_arr[0];
$user_pass = $user_data_arr[1];
$submit_pass = $_POST["pass"];
$submit_name = $_POST["user"];
if(!strcmp($submit_name,$user_name) && !strcmp($submit_pass,$user_pass)){
$_SESSION['flag'] = 1;
redirect("index.php");
}
else{
$_SESSION['flag'] = 0;
redirect("admin.php");
}
break;
case logout:
$_SESSION['flag'] = 0;
redirect("index.php");
break;
}
require("footer.htm");
function redirect($send_to) {
echo "<script language='JavaScript'>\n";
echo "<!--\n";
echo "function redirect(){\n";
echo "window.location = '$send_to'}\n";
echo "setTimeout(\"redirect();\", 1)\n";
echo "// -->\n";
echo "</script>\n";
}
?>
<?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
require("header.htm");
echo "<table width=\"75%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"><tr>";
echo "<td width=\"21\"><img src=\"images/news_left.jpg\" width=\"21\" height=\"43\" alt=\"left side news title bar\"></td>";
echo "<td align=\"center\" background=\"images/news_bg.jpg\"> </td>";
echo "<td align=\"right\" width=\"24\"><img src=\"images/news_right.jpg\" width=\"24\" height=\"43\" alt=\"right side news title bar\"></td></tr></table>";
switch($action) {
default:
echo "<form name=\"form1\" method=\"post\" action=\"admin.php?action=login\">\n";
echo "<p class=\"title\">Username: <input type=\"text\" name=\"user\" size=\"20\"><br>\n";
echo "Password: <input type=\"password\" name=\"pass\" size=\"20\"><br><br>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Login\"></p></form>";
break;
case login:
$user_file = fopen("db/users.txt", "r");
$user_line = fgets($user_file);
$user_data_arr = explode("|", $user_line);
$user_name = $user_data_arr[0];
$user_pass = $user_data_arr[1];
$submit_pass = $_POST["pass"];
$submit_name = $_POST["user"];
if(!strcmp($submit_name,$user_name) && !strcmp($submit_pass,$user_pass)){
$_SESSION['flag'] = 1;
redirect("index.php");
}
else{
$_SESSION['flag'] = 0;
redirect("admin.php");
}
break;
case logout:
$_SESSION['flag'] = 0;
redirect("index.php");
break;
}
require("footer.htm");
function redirect($send_to) {
echo "<script language='JavaScript'>\n";
echo "<!--\n";
echo "function redirect(){\n";
echo "window.location = '$send_to'}\n";
echo "setTimeout(\"redirect();\", 1)\n";
echo "// -->\n";
echo "</script>\n";
}
?>