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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号