我有一个account.php页面,用户可以在其中更改mySQL数据库中保存的帐户信息。当我点击链接时,我收到一条消息,说“在您访问此页面之前返回并登录!”我的代码如下。
pro.php(页面定向登录后)
<?php
//STEP 1 Connect To Database
$connect = mysql_connect("Localhost","mlec2013_danny","8764963d");
if (!$connect)
{
die("MySQL could not connect!");
}
$DB = mysql_select_db('mlec2013_database');
if(!$DB)
{
die("MySQL could not select Database!");
}
//STEP 2 Declare Variables
$Name = $_POST['username'];
$Pass = $_POST['password'];
$Query = mysql_query("SELECT * FROM Users WHERE Username='$Name' AND Password='$Pass'");
$NumRows = mysql_num_rows($Query);
$_SESSION['username'] = $Name;
$_SESSION['password'] = $Pass;
//STEP 3 Check to See If User Entered All Of The Information
if(empty($_SESSION['username']) || empty($_SESSION['password']))
{
die("Go back and login before you visit this page!");
}
if($Name && $Pass == "")
{
die("Please enter in a name and password!");
}
if($Name == "")
{
die("Please enter your name!" . "</br>");
}
if($Pass == "")
{
die("Please enter a password!");
echo "</br>";
}
//STEP 4 Check Username And Password With The MySQL Database
if($NumRows != 0)
{
while($Row = mysql_fetch_assoc($Query))
{
$Database_Name = $Row['username'];
$Database_Pass = $Row['password'];
}
}
else
{
die("Incorrect Username or Password!");
}
//end of PHP scripting. Information displayed below is in the form of HTML, CSS, or Javascript.
?>
account.php
<?php require_once('../../Connections/mySQL.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE Users SET first_name=%s, last_name=%s, mdcps_idnumber=%s, phone=%s, Username=%s, Password=%s, email=%s, class_code=%s WHERE id=%s",
GetSQLValueString($_POST['first_name'], "text"),
GetSQLValueString($_POST['last_name'], "text"),
GetSQLValueString($_POST['mdcps_idnumber'], "text"),
GetSQLValueString($_POST['phone'], "text"),
GetSQLValueString($_POST['Username'], "text"),
GetSQLValueString($_POST['Password'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['class_code'], "int"),
GetSQLValueString($_POST['id'], "int"));
mysql_select_db($database_mySQL, $mySQL);
$Result1 = mysql_query($updateSQL, $mySQL) or die(mysql_error());
$updateGoTo = "account.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
$colname_Recordset1 = "-1";
if (isset($_GET['Username'])) {
$colname_Recordset1 = $_GET['Username'];
}
mysql_select_db($database_mySQL, $mySQL);
$query_Recordset1 = sprintf("SELECT * FROM Users WHERE Username = %s", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $mySQL) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$maxRows_DetailRS1 = 10;
$pageNum_DetailRS1 = 0;
if (isset($_GET['pageNum_DetailRS1'])) {
$pageNum_DetailRS1 = $_GET['pageNum_DetailRS1'];
}
$startRow_DetailRS1 = $pageNum_DetailRS1 * $maxRows_DetailRS1;
$colname_DetailRS1 = "-1";
if (isset($_GET['recordID'])) {
$colname_DetailRS1 = $_GET['recordID'];
}
mysql_select_db($database_mySQL, $mySQL);
$query_DetailRS1 = sprintf("SELECT * FROM Users WHERE id = %s", GetSQLValueString($colname_DetailRS1, "int"));
$query_limit_DetailRS1 = sprintf("%s LIMIT %d, %d", $query_DetailRS1, $startRow_DetailRS1, $maxRows_DetailRS1);
$DetailRS1 = mysql_query($query_limit_DetailRS1, $mySQL) or die(mysql_error());
$row_DetailRS1 = mysql_fetch_assoc($DetailRS1);
if (isset($_GET['totalRows_DetailRS1'])) {
$totalRows_DetailRS1 = $_GET['totalRows_DetailRS1'];
} else {
$all_DetailRS1 = mysql_query($query_DetailRS1);
$totalRows_DetailRS1 = mysql_num_rows($all_DetailRS1);
}
$totalPages_DetailRS1 = ceil($totalRows_DetailRS1/$maxRows_DetailRS1)-1;
//STEP 1 Connect To Database
$connect = mysql_connect("Localhost","mlec2013_danny","8764963d");
if (!$connect)
{
die("MySQL could not connect!");
}
$DB = mysql_select_db('mlec2013_database');
if(!$DB)
{
die("MySQL could not select Database!");
}
//STEP 2 Declare Variables
$Name = $_POST['username'];
$Pass = $_POST['password'];
$Query = mysql_query("SELECT * FROM Users WHERE Username='$Name' AND Password='$Pass'");
$NumRows = mysql_num_rows($Query);
$_SESSION['username'] = $Name;
$_SESSION['password'] = $Pass;
//STEP 3 Check to See If User Entered All Of The Information
//STEP 4 Check Username And Password With The MySQL Database
?>
答案 0 :(得分:1)
您需要在脚本的开头添加session_start()
。
答案 1 :(得分:0)
我没有在您的任何脚本上看到session_start()? session_start()需要在使用/访问会话的所有页面上使用。确保在将标题/输出发送到浏览器之前使用它。
答案 2 :(得分:0)
确保在每个使用会话的文档中打开PHP标记后立即添加session_start()
。