未定义的索引:sessionName

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (32)

您好我已经创建了一个登录页面

 <form method="POST" action="log.php">

<table border="1" cellpadding="4" cellspacing="0"
	style="font-family: arial; font-size: 15px; border: 0px; text-align: left; margin-top: 5px; background-color: transparent;"
	width="100%">
	<thead>
		<tr>
			<td style="border: 0px; width: 20%; background-color: transparent;">
				Username:</td>
			<td style="border: 0px; width: 30%; background-color: transparent;">
				<input type="text" name="email" id="email" class="form-control"
				autocomplete="off" required="">
			</td>
		</tr>
	</thead>
</table>

<table border="1" cellpadding="4" cellspacing="0"
	style="font-family: arial; font-size: 15px; border: 0px; text-align: left; margin-top: 5px; background-color: transparent;"
	width="100%">
	<thead>
		<tr>
			<td style="border: 0px; width: 20%; background-color: transparent;">
				Password:</td>
			<td style="border: 0px; width: 30%; background-color: transparent;">
				<input type="password" name="pass" id="pass" class="form-control"
				autocomplete="off" required>
			</td>
		</tr>
	</thead>
</table>
</div>


<div class="spacer-20"></div>

</div>
<!-- Social Signup -->
<div class="social-signup">
	<span class="or-break"></span>
	<center>
	<button type="submit" name="login" class="btn btn-primary">
		<span class="glyphicon glyphicon-log-in"></span> Login
	</button>
	No account? <a href="joinus.php"> Sign up</a>

</form>

登录处理器如下

<?php
// Login Controller
// require('config/config.php');
include './config/config.php';
$userName = $passWord = $name = "";

if (isset ( $_POST ['login'] )) {
	$userName = mysqli_real_escape_string ( $con, $_POST ['email'] );
	$password = mysqli_real_escape_string ( $con, $_POST ['pass'] );
	$pass = md5 ( $password );
	// Block handles Doctor Login using the email
	$get_doc = "SELECT * FROM doctor_registration WHERE email='$userName' AND password='$pass'";
	$run_doc = mysqli_query ( $con, $get_doc );
	$rows = mysqli_fetch_array ( $run_doc );
	$docId = $rows ['doc_id'];
	$docEmail = $rows ['email'];
	$name = $rows ['first_name'] . " " . $rows ['last_name'];
	
	if ($userName == $docEmail) {
		$_SESSION ['sessionName'] = $name;
		$_SESSION ['sessionId'] = $docId;
		echo "<script>window.open('doctor_dashboard.php','_self')</script>";
	} else {
		// Block handles user Login using the username
		$get_user = "SELECT * FROM users WHERE username='$userName' AND password='$pass'";
		$run_user = mysqli_query ( $con, $get_user );
		$rows = mysqli_fetch_array ( $run_user );
		$userId = $rows ['user_id'];
		$uname = $rows ['username'];
		if ($userName == $uname) {
			$_SESSION ['sessionName'] = $uname;
			$_SESSION ['sessionId'] = $userId;
			echo "<script>window.open('user_dashboard.php','_self')</script>";
		} else {
			echo "<script>alert('Passowrd or username is not correct!')</script>";
		}
	}
}
?>

对于仪表板,处理器检查电子邮件或用户名并重定向到其中一个仪表板。

我有两个仪表板,其中包含用于启动和存储会话的代码

<?php
	session_start ();
	include './config/config.php';
	include './updateFunction.php';
	include './InsertFunction.php';
	
	if (isset ( $_SESSION ['sessionName'] ) && ($_SESSION ['sessionId'])) {
		header ( "location:index.php" );
	}
?>

每次我尝试在两个仪表板中登录时都会收到错误消息:

未定义的索引:sessionName

提问于
用户回答回答于

你应该在使用session_start()之前使用$_SESSION['sessionName'] = $name

在登录处理器中。

所以最好你应该让session_start()在页面的开头使用

用户回答回答于

把session_start(); 在<?php之后的config.php中

然后在<?php之后的每个页面上调用config.php

你得到会话错误,因为你没有使用session_start(); 在你的文件中

所属标签

可能回答问题的人

  • 西风

    renzha.net · 站长 (已认证)

    7 粉丝1 提问9 回答
  • 四无君

    0 粉丝0 提问3 回答
  • 旺仔小小鹿

    社区 · 运营 (已认证)

    46 粉丝0 提问2 回答
  • 拉布拉多拉不多

    1 粉丝0 提问2 回答

扫码关注云+社区

领取腾讯云代金券