2012-03-25 11 views
0

データベースに接続し、それに対してチェックするが、リダイレクトが機能していない(login.phpからloggedin.phpへ)ログインするウェブサイトにログインしようとしています。私は自分自身のコードを読んでいる証拠にぞっとしているし、しばらくの間、サークルに回っています。誰かが助けることができたら、私はとても感謝しています!前もって感謝します。ログインが正しくリダイレ​​クトされていません

Login_page.inc.php

<?php # Script 11.1 - login_page.inc.php 

// this page prints any errors associated with logging in 
//and creates te entire login page, including the fom 

//include the header: 

$page_title = 'Login'; 

include ('includes/header.html'); 

//print any error messages if they exist: 

if (!empty($errors)) 
{ 
echo '<h1>Error!</h1> 

<p class="error">The following error(s) occurred:</br>'; 
foreach ($errors as $msg) 
    { 
    echo "- $msg</br>\n"; 
    } 
echo '</p><p>Please try again.</p>'; 
} 

//display form 
?> 
<h1>Login</h1> 

    <form action=login.php" method="post"> 

     <p>Email Address: <input type="text" name="email" size="20" maxlength="80"/></p> 

     <p>Password: <input type="password" name="pass" size="20" maxlength="20"/></p> 

     <p><input type="submit" name="submit" value="Login"/></p> 

     <input type="hidden" name="submitted" value="TRUE"/> 

    </form> 

<?php //include the footer: 

include ('includes/footer.html'); 

?> 

Loggedin.php

<?php # loggedin.php 

//this is where the user is directed from login.php 
session_start(); 
//if no cookie is present redirect the user: 
//if (!isset($_COOKIE['user_id'])) 
if (!isset($_SESSION['user_id'])) 
{ 
    //the functions need to create an absolute url 
    require_once ('includes/login_functions.inc.php'); 

    $url = absolute_url(); 
    header("Location: $url"); 
    exit(); //exit script 
} 

//set the page title and include the header 
$page_title = 'Logged in.'; 
include ('includes/header.html'); 

//welcome message 
echo "<h1>Logged in!</h1> 
<p>You have successfully logged in, {$_SESSION['first_name']}!</p> 

<p><a href=\"logout.php\">Logout</a></p>"; 

include ('includes/footer.html'); 

?> 

Login.php

<?php # login.php 
//this page processes the login form submission 
//upon successful login the user's redirected 
//two include files are needed for this 
//send nothing to the web browser prior to the setcookie() lines 

//check if the form has been submitted: 
if (isset($_POST['submitted'])) 
{ 
    //for processing the login: 
    require_once ('includes/login_functions.inc.php'); 

    //need the database connection: 
    require_once ('includes/mysqli_connect.php'); 

    //check the login 
    list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']); 

    if($check) 
    { 
     /*ok, set cookies to last one hour after it is set 
     setcookie ('user_id', $data ['user_id'], time()+3600, '/', '', 0, 0); 

     setcookie ('first_name', $data ['first_name'], time()+3600, '/', '', 0, 0);*/ 

     session_start(); 

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

     //redirect 
     $url = absolute_url ('loggedin.php'); 
     header("Location: $url"); 
     exit(); //quit the script 
    } 

    else 
    { 
     //assign errors to $data for error reporting in the login_page.inc.php 
     $errors = $data; 
    } 

    mysqli_close($dbc); //close the database connection 
} //end of main submit condition 

include ('includes/login_page.inc.php'); 



?> 

Login_functions.php

<?php #- login_functions.inc.php 

//this page defines two functions used by the login/logout process. 

/*this function determines and returns an absolte URL 
*takes one argument: the page that concludes the URL 
*the arguement defaults to index.php 
*/ 

function absolute_url ($page = 'index.php') 
{ 

//start defining the URL. . . 

//URL is http:// plus the host name plus current directory: 

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

//remove any trailing slashes: 
$url = rtrim($url, '/\\'); 

//adding the page. . . 
$url.= '/' . $page; 

//return to the url 
return $url; 

} //end of the absolute_url function 

/* this function validates the form data (the email address and password) 

*if both are present the database is queried 

*this function requires a database connection 

* the function returns an array of information. including: 

* - a TRUE or FALSE variable indicating a success or failure 
* - an array of either errors or the database return result 

*/ 

function check_login($dbc, $email = '', $pass = '') 
{ 
    $errors = array(); //starting error array 

    //validate email address 

    if (empty($email)) 
    { 
    $errors[] = 'You forgot to enter your email address.'; 
    } 

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

    //validate the password 

    if (empty($pass)) 
    { 
    $errors[] = 'You forgot to enter your password.'; 
    } 

    else 
    { 
    $p = mysqli_real_escape_string($dbc, trim($pass)); 
    } 

    if (empty($errors)) 
    { 
    /*if everything's okay 

    *retrieve the user_id and the first_name for that 
    *email+password combination: 
    */ 

    $q = "SELECT user_id, first_name FROM site_users WHERE email='$e' AND pass=SHA1('$p')"; 

    $r = @mysqli_query ($dbc, $q); //run the query 

    //check the result and making sure that both fields are in the same row 
    if(mysqli_num_rows($r) ==1) 
    { 
    //fetch the record 
    $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); 

    //return true and the record: 
    return array(true, $row); 
    } 

    else 
    { 
    //not a match 
    $errors[] = 'The email address and password entered do not match those on file.'; 
    } 

    }//end of empty($errors) IF. 

    //return false and the errors: 
    return array(false, $errors); 
} //end of check_login() function 

?> 
+0

どのように動作していないのですか?どこにリダイレクトしますか?メインディレクター?またはloggedin.php? – riso

+0

どのリダイレクトに失敗しますか? 'if($ check)'の内側にある?そして、 'absolute_url()'の出力が正しいことを確認しましたか? –

+0

loggedin.phpは私がアクセスしようとしている場所です。 ログインしたユーザーにリダイレクトしようとすると、ページが存在しないことがわかります。私は何度も何度も見ていますが、なぜそうでないのか分かりません。 – PurpleSmurph

答えて

0

問題はSHA1内でSHA1が(40)強く、データベースが(20)に設定されていたデータベースと対話していました。迷惑な問題ですが、解決されていません。 Var_dumpは、入力された情報が正しいことを証明するためにデータベースと会話するために使用されましたが、データベースに保持されているパスワードは(20)、ログイン用に入力されたパスワードは(40)でした。

1

ファイルにブラウザに送られるコードが含まれている場合、PHPはすべてのヘッダを自動的に送信します。ヘッダーが送信された後、新しいヘッダーを送信できなくなり、Location:ヘッダーが削除されます。 PHPは通知をスローして、あなたのログでそれを探し、そして/または正しいerror_reportingフラグを設定する必要があります。

これには、改行やスペース、または?>タグブロックの後にあるものが含まれます。

+0

loggedin.phpの読み込みを停止しますか?現時点では、ログインしようとすると404エラーが発生します。私はPHPからこのようなエラーはありませんでした。 phpに比較的新しい。 – PurpleSmurph

+0

ああ、関連するエラー!もしあなたがそれらのことを伝えれば、私は起こっていたことをちょうど推測していました。両方のファイルが同じケースにあると確信していますか?ターゲットURLは正しいですか? Unixのようなウェブサーバーは大文字と小文字を区別する傾向があります –

+0

Sh * t、申し訳ありません!ただ編集しました。はい、ログインとログインの両方が同じディレクトリフォルダにあります。 – PurpleSmurph

0

あり、私が見ることができるものから、スクリプトと間違って何もここに...ではありませんが、あなたはまた、

<form action=login.php" method="post">

を交換する必要が

includes/header.html index.php

の内容のわかりません

<form action="login.php" method="post">

あなたのヘッダ(前に空白行を含めないでくださいたぶん私はより良い

おかげ :)

+0

それは問題の1つでした(恥ずかしい、私は自分のものを読んで証明に悪いです)。しかし、私は今、パスワードと電子メールが一致しないと言ってエラーを出しています。私は彼らが(私はphpmyadminを開いていて、それが一致しているのを見ることができる)と確信しています。 – PurpleSmurph

+0

エラーがある場合は教えてください??? – Baba

+0

次のエラーが発生しました: - 入力した電子メールアドレスとパスワードがファイル上のものと一致しません。電子メールとパスワードの両方が正しいにもかかわらず、データベーステーブルが一致している場所がわかるので、何がうまくいかないのか分かりません。 – PurpleSmurph

0

をお手伝いをすることができます..あなたが見ているエラーを貼り付けることができた場合は「場所:$のURL」 )。これによりヘッダーの送信が妨げられ、リダイレクトを取得して、どこに行きたいかを指定することができなくなります。

関連する問題