Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session doesn't always start 1

Status
Not open for further replies.

jason246day

Programmer
Jul 9, 2003
127
US
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\">&nbsp;</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: &nbsp;<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";
}
?>
 
Looks like I spoke too soon. I was working fine, but then stopped. I've replaced the javascript redirect with the meta tag redirect. Here is my code, does anyone know why its not working???

<?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\">&nbsp;</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: &nbsp;<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 "<head><meta http-equiv=\"Refresh\" content=\"0;url=$send_to\"></head>";
}

?>
 
Okay, I didn't change any of the code, and its back to working again. Is this a normal type thing for sessions? I have used them before, but am by no means an expert. Is there any type of error reporting I can run to see what is happening when my session variable is not being set???
 
Sometimes during testing of session-manipulating code, particularly when testing that code with IE, the browser can get a little confused.

Some browsers give you the ability to selectively examine and delete cookies on your machine. I've found that deleting cookies from that server for that site can clear the problem up.

Then again, it could be something else.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I think I have figured it out. all i needed to do was set some default values. I stuck this line of code in, and I saw all sorts of warnings.

error_reporting(E_ALL);

Once I fixed all of the warnings, everything seemed to be working great. I did this earlier in the day, and have been periodically checking the script to see if it is still working, as it seems to be good. Hopefully its good to go now.

thanks for the help.
 
It stopped working again. Now I am getting this error

Notice: Undefined index: flag in index.php on line 29

flag is the variable i use to keep track of whether the admin is logged in or not. Do I need to declare or initialize this in some way. The only reason I'm getting this error is because that variable is not getting set. But I don't know why that is happening.
 
this line of code solved my problem

ini_set('session.use_trans_sid', false);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top