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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable