Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

php - Action script for login form not working

having problems understanding why my script to login will not work, so its a simple login script that checks the users and fields as expected yet when it does the logic it does not seem to be loggin in the users :S

action script:

    <?php

        if ( $SERVER[ 'REQUEST_METHOD' ] == 'POST' )

        {

        require ( 'connect_db.php' );

        require ( 'login_tools.php' );

        list ( $check , $data ) =

            validate ( $dbc , $_POST[ 'email' ] , $_POST [ 'pass' ] ) ;

        if ( $check )

        {
            session_start() ;

            $_SESSION[ 'user_id' ] = $data [ 'user_id' ] ;
            $_SESSION[ 'first_name' ] = $data  [ 'first_name' ] ;
            $_SESSION[ 'last_name' ] = $data  [ 'last_name' ] ;

            load ('home.php');

        }

        else { $errors = $data ; }

        mysqli_close( $dbc );


        }


?>

An action script to process the login:

<?php

function load( $page = 'login.php')

    {

    $url = 'http://' . $SERVER['HTTP_HOST'] . 
                        dirname( $_SERVER ['PHP_SELF'] );

    $url = rtrim( $url , '/\' );
    $url = '/' . $page ;

    header ( "location: $url" );
    exit();     

    }

function validate( $dbc , $email = ',$pwd = ')


    {

    $errors = array();

    if (empty($email))

    { $errors[] = 'Enter your email address.' ; }

    else

    { $e = mysqli_real_escape_string( $dbc , trim( $email ) ) ; }

    if (empty($pwd))

    { $errors[] = 'Enter your password.' ; }

    else

    { $e = mysqli_real_escape_string( $dbc , trim( $pwd ) ) ; } 

    if ( empty( $errors ) )

    {

    $q = "SELECT user_id, first_name, last_name FROM users WHERE enail = '$e' AND pass = SHA1( '$p' )";

    $r = mysqli_query ( $dbc , $q ) ;

    if ( mysqli_num_rows( $r ) == 1 )

    {

    $row = mysqli_fetch_array ( $r , MYSQLI_ASSOC );
    return array (true , $row );

    }

    else

    {

    $errors[] = 'Email address and password not found.' ;

    }

    return array( false , $errors) ; }

    }
?>

And it will land here...

<?php

session_start();

    if ( !isset( $_SESSION[ 'user_id' ] ) )

    {

        require ( 'login_tools.php' ) ;
        load() ;

    }

    $page_title = 'Home' ;


    echo'<p>
    <a href="goodbye.php">logout</a>
    </p> ';

?>

The login script tried to execute login_action.php but dosnt move from there...I have no syntax errors though?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You misspelled $_SERVER variable - there is not such thing like $SERVER

EDIT

login_tools.php

function validate($dbc, $email = ',$pwd = ')

should be:

function validate($dbc, $email = '' , $pwd = '')

next:

$e = mysqli_real_escape_string($dbc, trim($pwd));

should be:

$p = mysqli_real_escape_string($dbc, trim($pwd));

and return statement move after if statement:

if (empty($errors)) {
    ...
}

return array(false, $errors);

I hope that you're playing around with PHP or something, beacuse this is really bad code. But you know that, right?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...