I wrote this application about a year ago where people register, log in and make picks based on what they thought would happen on future episodes of Survivor. Worked pretty well, though there were minor bugs.
I copied much of the code to do something similar with this year's NFL season and I am running into about a 10% group of users who can successfully register and log in but when they go to make their picks, they are no longer logged in. They log in, go to their menu, click on Make Picks and are logged out again. About 60% of the users have registered, logged in successfully and made their picks successfully.
Much of the code I have used is a third generation borrow from the very helpful book, PHP and MySQL Web Development by Luke Welling and Laura Thomson - chapter 24.
I have used several small single-function files and one big functions file.
I call the database and table connections from the functions file.
Important code from the functions.php file:
(very top of the file:
require_once("db_fns.php");
session_start();
... (lots of HTML code - log-in and registration forms, etc)
//AFTER LOG-IN, THIS MENU IS DISPLAYED:
function display_user_menu()
{
//$valid_user = $username;
session_register("valid_user");
echo "<hr>";
echo "<a href='member.php' class='link'>Home</a><br>";
echo "<a href='change_passwd_form.php' class='link'>Change password</a><br>";
echo "<a href='rules.php' class='link' target='_blank'>Rules and Scoring</a><br>";
echo "<a href='mailto:cygnus@cygnus-study.com?subject=Football Quick Pick' class='link'>Questions & Bug Reports</a><br>";
echo "<a href='logout.php' class='link'>Logout</a><br>";
echo "<hr>";
echo "<a href='scoreboard.php' class='link'>Scoreboard</a><br>";
$now = time();
$deadline = mktime(20,0,0,9,4,2002);
if ($now >= $deadline)
{
echo "<span class='text'>We're sorry. Picks are closed until next week.</span><br>";
} else {
echo "<a href='make_picks.php' class='link'>Make/Change Picks</a><br>";
}
echo "<a href='view_picks.php' class='link'>View Picks</a><br>";
echo "<hr>";
}
... (HTML Make Picks form. Form Action=submit_picks.php or changed_picks.php)
//SUBMIT PICKS PHP
function submit_picks($valid_user, $submitted, $game1, $game2, $game3, $game4, $game5,
$game6, $game7, $game8, $game9, $game10, $game11,
$game12, $game13, $game14, $game15, $game16, $MNF_score1, $MNF_score2)
// submit NEW picks
{
global $valid_user;
if (!($conn = db_connect()))
return false;
$result = mysql_query("insert into week1 values
('$valid_user', '$submitted', '$game1', '$game2', '$game3', '$game4', '$game5',
'$game6', '$game7', '$game8', '$game9', '$game10', '$game11',
'$game12', '$game13', '$game14', '$game15', '$game16', '$MNF_score1', '$MNF_score2')");
if (!$result)
return "<p class='text'>Could not register your picks in the database - please try again later.</p>";
return true;
}
... (I DUPLICATE THE MAKE PICKS CODE FOR AN EDIT PICKS FUNCTION)
//REGISTER, LOG-IN and VALIDATION CODE
function register($username, $email, $password)
// register new person with db
// return true or error message
{
// connect to db
$conn = db_connect();
if (!$conn)
return "<p class='text'>Could not connect to database server - please try later.</p>";
// check if username is unique
$result = mysql_query("select * from user where username='$username'");
if (!$result)
return "<p class='text'>Could not execute query</p>";
if (mysql_num_rows($result)>0)
return "<p class='text'>That username is taken - go back and choose another one.</p>";
// if ok, put in db
$result = mysql_query("insert into user values
('$username', password('$password'), '$email')");
if (!$result)
return "<p class='text'>Could not register you in database - please try again later.</p>";
$result = mysql_query("insert into scores values
('$username', '0.0','0.0')");
if (!$result)
return "<p class='text'>Could not register you in database - please try again later.</p>";
return true;
}
function login($username, $password)
// check username and password with db
// if yes, return true
// else return false
{
// connect to db
$conn = db_connect();
if (!$conn)
return 0;
// check if username is unique
$result = mysql_query("select * from user
where username='$username'
and passwd = password('$password')");
if (!$result)
return 0;
if (mysql_num_rows($result)>0)
{
return 1;
}
else
return 0;
}
function check_valid_user()
// see if somebody is logged in and notify them if not
{
global $valid_user;
if (session_is_registered("valid_user"))
{
db_connect();
$results = mysql_query( "select * from scores where username = '$valid_user'");
if (!$results)
return "0";
$num_results = mysql_num_rows($results);
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($results);
}
$total_score = $row["total_score"];
echo "<br>";
echo "<table width='400' cellpadding='5' cellspacing='0' border='0' bgcolor='#CCCCCC'><tr><td>";
echo "<p class='text'>Logged in as $valid_user.</p>";
echo "<p class='text'>Total score: ".$total_score."<br>";
echo "Check your place <a href='scoreboard.php' class='link'>here</a>.</p>";
echo "<p class='text'>Current time is:<br><b>".date("l, F jS, Y H:i a")." PT</b></p>";
echo "<p class='redtext'>Deadline for this week's picks:<br>Wednesday, September 4th, 2002 at 20:00 PT.</p>";
echo "</td></tr></table>";
} else {
// they are not logged in
// do_html_header("Problem:");
echo "<p class='text'>You are not logged in.<br><a href='login.php' class='link'>Login</a>";
do_html_footer();
exit;
}
}
I know that that is confusing.
Here are pertinent files:
File called when user clicks on Make Picks:
make_picks.php
<?
session_start();
require_once("functions.php");
do_html_header("Make Picks");
check_valid_user();
display_user_menu();
display_make_picks_form();
do_html_footer();
?>
I guess it boils down to this:
Why are some people logged out between successfully logging in and loading the Make_Picks page?
If you want to play or just join to see if it happens to you, http://www.survivoraddicts.com/fdbp/login.php
Thank you.
Cygnus