dtsc1684 2013-10-08 14:07
浏览 55
已采纳

登录脚本不起作用 - PHP

This login script doesn't seem to work. I checked if it gets past the if statement, and it does. What else can be the problem?

the script that handles the login:

<?php
    include("config.php");

    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword']; 

    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $sql="SELECT * FROM " .$members. " WHERE BINARY `username`= '".$myusername."' and BINARY `password`= '".$mypassword."'";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);

    $row = mysql_fetch_array( $result );

    if($count==1){
        $_SESSION['username'] = $myusername;
        $_SESSION['password'] = $mypassword;
        $_SESSION['privileges'] = $row['privileges'];
        $_SESSION['email'] = $row['email'];
        header("location:index.php");
    }
?>

The script that checks if the user logged in on index.php:

<?
    session_start();
    if(!isset($_SESSION['username'])){
        header("location:login.php");
    }       
?>
  • 写回答

2条回答 默认 最新

  • dtwkt46424 2013-10-08 14:30
    关注

    Improve your code like this

    login-exec.php

        <?php
    //Start Session
    session_start();
    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }
    
    //Sanitize the POST values
    $myusername = clean($_POST['myusername']);
    $mypassword = clean($_POST['mypassword']);
    
    
    //Create query
    $qry="SELECT * FROM " .$members. " WHERE `username`='".$myusername."' AND `password`='".$mypassword."'";
    $result=mysql_query($qry);
    
    //Check whether the query was successful or not
    if($result) {
        if(mysql_num_rows($result) == 1) {
            //Login Successful
            session_regenerate_id();
            $member = mysql_fetch_assoc($result);
            $_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
            $_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
            $_SESSION['SESS_LAST_NAME'] = $member['lastname'];
            $_SESSION['SESS_EMAIL'] = $member['email'];
            $_SESSION['SESS_USER_NAME'] = $member['username'];
            session_write_close();
            header("location: index.php");
            exit();
        }else {
            //Login failed
            header("location: login-failed.php");
            exit();
        }
    }else {
        die("Query failed");
    }
    
    ?>
    

    And keep your index.php into this

      <?php session_start(); ?>
      <?php 
    require_once('safe.php');
     ?>
    

    Create a php page input this code for check User

    <?php
    //Start session
    session_start();
    
    //Check whether the session variable SESS_MEMBER_ID is present or not
    if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
        header("location: access-denied.php");
        exit();
    }
    

    ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题