I have created a login script that logs into an app, however when I log in with a players detail, it does not match the player that's already playing in the home page and play pages the player_id
is set to 1
, this is the the home page, where the player_id
is set to 1
and also in the play.php
page the player_id
is set to 1
.
I want to change it so that when I log in the player I use to login should match the player playing, I have three players. This is the home.php
page below.
<?php
//include ("session.php");
session_start();
# check if player id is set in query string
if (!isset($_GET['player_id'])) {
# it is not set, so make them player 1 by default
$player_id = 1;
/*
if (login_user = "sam"){
$player_id = 1;
}
if (login_user = "ben"){
$player_id = 2;
}
if (login_user = "jack"){
$player_id = 1;
}
*/
}
else {
# set player_id based on GET parameter (imagining it was passed by some login script)
$player_id = $_GET['player_id'];
}
# Define the SQL queries to run...
# This query will fetch the details about the player
$sql = "SELECT username, forename, surname FROM Player WHERE id=$player_id";
# Query the database
$result = mysqli_query($link, $sql);
# Get number of rows in result-set
$row_cnt = mysqli_num_rows($result);
# check if there are rows to display...
if ($row_cnt == 0) {
# if not, output a suitable message
$content .= "<p>Sorry, I don't know who you are...</p>";
} else {
# Otherwise set a variable with their name
while ($row = mysqli_fetch_assoc($result)) {
$forename = $row['forename'];
$username = $row['username'];
}
# free result set
mysqli_free_result($result);
# update content
$content .= "<h1>Welcome to Hang Man $forename!</h1>";
$content .= "<p>An exciting word game for 1 player.</p>";
$content .= "<p>You are playing as <strong>$username</strong></p>";
/*$content .="<<h2>welcome<"?php echo $login_session; ?></h2>*/
/*$content .= "<h3>< a href = "logout.php">Sign out</a></h3>*/
}
$login_session=$_SESSION['login_user'];
$login_session;
?>
<html">
<head>
<title>Welcome </title>
</head>
<body>
<h2><a href = "logout.php">Sign Out</a></h2>
</body>
</html>
Login page:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table align="center" bgcolor="#CCCCCC" border="0" cellpadding="0"
cellspacing="1" width="300">
<tr>
<td>
<form method="post" name="">
<table bgcolor="#FFFFFF" border="0" cellpadding="3"
cellspacing="1" width="100%">
<tr>
<td align="center" colspan="3"><strong>User
Login</strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input id="username" name=
"username" type="text"></td>
</tr>
<tr>
<td>Forename</td>
<td>:</td>
<td><input id="forename" name="forename"
"text"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input name="submit" type="submit"
"Login"> <input name="reset" type="reset"
"reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
if (isset($_POST['submit'])) {
include("db_connect1.php");
require "includes/functions.php";
session_start();
$username=$_POST['username'];
$forename=$_POST['forename'];
$player_id = $_SESSION['player_id'];
$_SESSION['login_user']=$username;
$query = mysql_query("SELECT username FROM Player WHERE username='$username' and forename='$forename'");
if (mysql_num_rows($query) != 0) {
echo "<script language='javascript' type='text/javascript'> location.href='index.php' </script>";
}
//if(mysql_num_rows($query) !=0){
//echo ".$_GET["playerid"]."
else {
echo "<script type='text/javascript'>alert('User Name Or Password Invalid!')</script>";
}
}
?>
</body>
</html>