duanchuonong5370 2014-02-02 08:50
浏览 121

用户/管理员通过PHP登录

My page works, in the sense that login will take me to index.php, but it will never re-direct to dashboard.php. I appreciate any input.

Database columns are: id | username | password | admin
There are only two types of account, the admin account is indicated by a 1

<?php

// Get username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// Connect to database
include "db_connect_service.php";

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$mypassword = crypt($mypassword, '$1$philipphp');

// Query
$result= $dbh->query("SELECT * FROM members WHERE username='$myusername' AND     password='$mypassword';");

// Mysql_num_row is counting table row
$count = $result->rowCount();

// Determine if user is "user" or "admin"
$usertype = $dbh->query("SELECT admin FROM members WHERE username='$myusername';");
$row = mysql_fetch_row($usertype);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1) {

// Start session, register $myusername, $mypassword and redirect to login_success.php
session_start();
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['admin'] = $row;

// Redirect to appropriate page depending on user rights. Indicator 0 for user, 1 for admin.
if ($_SESSION['admin'] == 1){
    header("location:dashboard.php");
    }
else {
    header("location:index.php");
    }
$dbh = null;
}
else {
echo "Wrong Username or Password";
}
?>
  • 写回答

3条回答 默认 最新

  • doujiang1993 2014-02-02 08:53
    关注

    $_SESSION['admin'] = $row; -> you are assign a row. but checking with 1.

    I think your admin field is user type in your table. So you have to get user type and compare in if condition

    Do like this

    $_SESSION['admin'] = $row["admin"];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答