未定义的索引:sessionName

时间:2019-03-04 18:20:57

标签: php jquery html mysqli

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

 <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

2 个答案:

答案 0 :(得分:0)

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

在登录处理器中。

因此,最好在页面开头使用session_start()

答案 1 :(得分:0)

session_start();之后将<?php放入config.php

然后在<?php

之后的每个页面上调用config.php

由于没有在文件中使用session_start();,因此出现会话错误