我有登录系统,当用户成功登录时会应用会话。这是你知道很久的事情,对吗?登录就像一个魅力,但之后不能应用任何会话。我试过var_dump它,print_r它,并且没有任何成功地回应它但没有任何错误。有人可以帮助兄弟吗?
代码
<?php
require_once $_SERVER["DOCUMENT_ROOT"] . '/project-school/config/init.php';
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 'On');
if (isset($_POST['submit-admin-login']))
{
$username=db_escape($koneksi, $_POST['username']);
$password=db_escape($koneksi, md5($_POST['password']));
if(empty($username) || empty($password))
{
$_SESSION['pesan'] = 'Username dan Password Wajib Diisi Dengan Benar';
$_SESSION['alert'] = 'danger';
header('location:../login.php');
}
else
{
$sqladmin= "SELECT * FROM admin WHERE username='$username' AND password='$password'";
$queryadmin= mysqli_query($koneksi, $sqladmin) or die(mysqli_error($koneksi));
$resultadmin= mysqli_num_rows($queryadmin);
$sqlpengajar= "SELECT * FROM pengajar WHERE username_login='$username' AND password_login='$password'";
$querypengajar= mysqli_query($koneksi, $sqlpengajar) or die(mysqli_error($koneksi));
$resultpengajar= mysqli_num_rows($querypengajar);
if($resultadmin > 0)
{
while ($row = mysqli_fetch_array($queryadmin)) {
$_SESSION['username_admin'] = $row['username'];
$_SESSION['level'] = $row['level'];
}
echo $_SESSION['username_admin'];
header('Location: ../view/admin-dashboard.php');
}
elseif($resultpengajar > 0)
{
while ($row = mysqli_fetch_array($querypengajar)) {
$_SESSION['nip'] = $row['nip'];
$_SESSION['nama_lengkap'] = $row['nama_lengkap'];
$_SESSION['level'] = $row['level'];
$_SESSION['username_pengajar'] = $row['username_login'];
$_SESSION['alamat'] = $row['alamat'];
$_SESSION['tempat_lahir'] = $row['tempat_lahir'];
$_SESSION['tgl_lahir'] = $row['tgl_lahir'];
$_SESSION['jenis_kelamin'] = $row['jenis_kelamin'];
$_SESSION['agama'] = $row['agama'];
$_SESSION['foto'] = $row['foto'];
}
echo $_SESSION['username_pengajar'];
header('Location: ../view/pengajar-dashboard.php');
}
else
{
$_SESSION['pesan'] = 'Username atau Password Anda Salah';
$_SESSION['alert'] = 'danger';
header('Location: ../login.php');
}
}
}
会话在num_rows
之后开始答案 0 :(得分:1)
尝试在所需语句之前启动会话。像
<?php
session_start();
require_once $_SERVER["DOCUMENT_ROOT"] . '/project-school/config/init.php';
每当你打算使用会话时,总是在第一次开始会话,否则php不会读取它。
您的sql注射风险很高。了解准备好的语句,以防止sql注入