2017-10-21 208 views
0

我有,我沒有使用數據庫,因爲這是一所學校分配的一些問題,與多個用戶的PHP我的登錄功能。PHP登錄功能沒有數據庫,重定向錯誤

所以分配如下:「創建一個用戶名作爲使用password_hash密鑰和加密的密碼關聯數組。一旦用戶登錄,應打印出用戶名。」我們得到了一個我們應該使用的模板,我將粘貼所有必要的代碼。因此,我遇到的問題是,當我嘗試登錄爲用戶它使重定向我,讓我得到了「過多的重定向」錯誤。

的login.php

<?php 
session_start(); 

if(isset($_POST['password'],$_POST['username'])){ 

    include("pwd.php"); 
    include("user.php"); 

    $key = $_POST['username']; 

    if(isset($user[$key])){ 

     $_SESSION['inloggad'] = true; // Användaren har anget rätt uppgifter. 
     $_SESSION['user'] = $_POST['username']; 

    } 
} 


if(isset($_SESSION['inloggad'])){ 
    header("Location: index.php"); 
} 
else{ 
    echo "<h1>Vänligen logga in!</h1>"; 
} 
?> 

的login.html

<!DOCTYPE html> 
<html lang="sv"> 

<head> 
    <meta charset="utf-8" > 
    <title>Sessioner</title> 
</head> 

<body> 
    <form method="post" action="login.php"> 
     Username: <input type="username" name="username" size="20" /><br /> 
<br> 
     Password: <input type="password" name="password" size="20" /><br /> 
     <input type="submit" value="Logga in" name="login"/> 
    </form> 
</body> 
</html> 

user.php的

<?php 
$user['admin'] = '$2y$10$9NyoNcqG9sh0KOrVnUXLr.KscgDy0L1S0klYXK67oxVBVsElbbGja'; 

$user['hank'] = '$2y$10$tQNcTINMIcotw0IczQ1nTuOVRIpbuqh5M/k.mLpz7ZiZl8q2WA0Cy'; 

$user['elias'] = '$2y$10$tQNcTINMIcotw0IczQ1nTuOVRIpbuqh5M/k.mLpz7ZiZl8q2WA0Cy'; 

?> 

start.php

<h1>Välkommen</h1> 
<?php 
    include("login.php"); 

    if(isset($_POST['password'],$_POST['username'])){ 

    echo '<h1>' . $_POST['username'] . '</h1>'; 
    } 
?> 

的index.php

<?php session_start(); ?> 
<!doctype html> 
<html lang="sv"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Länka in med PHP</title> 
    <link href="css/styleSheet.css" rel="stylesheet" type="text/css"> 
</head> 

<body> 
    <div id="wrapper"> 
     <header> 
     <?php include("header.php"); ?> 
     </header><!-- header --> 

     <section id="leftColumn"> 
      <nav> 
      <?php 
      if(isset($_SESSION['inloggad'])){ 
       include("meny.php"); 
      }else{ 
       include("login.html"); 
      } 


      ?> 
      </nav> 
      <aside> 
      <?php include("aside.php"); ?> 
      </aside> 
     </section><!-- End leftColumn --> 

     <main> 
     <section> 
     <!-- Lägg in innehållet här --> 
     <?php 
      $page = "start"; 
      if(isset($_GET['page'])) 
       $page = $_GET['page']; 

       switch($page){ 
       case 'blogg': include('blogg.php'); 
         break; 
       case 'bilder': include('bilder.php'); 
         break; 
       case 'kontakt': include('kontakt.php'); 
         break; 
       case 'klotter': include('klotter.php'); 
         break; 
       default: include('start.php'); 
      } 
     ?> 

     </section> 
     </main><!-- End main --> 

     <footer> 
     <?php include('footer.php'); ?> 
     </footer><!-- End footer --> 
    </div><!-- End wrapper --> 
</body> 
</html> 

stylesheet.css中

@CHARSET "UTF-8"; 
* { 
    margin:0; 
    padding:0; 
    font-family:Verdana, Geneva, sans-serif; 
} 
body{ 
    font-size: 100%; 
} 
p { 
    font-size: 0.8em; 
    margin-bottom: 10px; 
    margin-top: 5px; 
    margin-right: 10px; 
    text-align: justify; 
} 

/* Wrapper */ 
#wrapper { 
    width: 800px; 
    margin-left: auto; 
    margin-right:auto; 
    margin-top:10px; 
    border: 2px solid rgba(0,0,0,0.8); 
} 
/* End wrapper */ 

/* Header */ 
header { 
    text-align:center; 
    height: 60px; 
    background-image: url("../bilder/bgImg.png"); 

    color: white; 
} 
header h1{ 
    font-family: Arial; 
    font-size: 1.9em; 
    padding-top: 0.25em; 
} 

header time{ 
    float: right; 
    margin-right: 2em; 
    font-size: 0.8em; 
} 
/* End header */ 

nav{ 
    border-radius: 5px; /* CSS3 */ 
    border: 1px solid #999; 
    padding: 4px; 
    margin-bottom:5px; 
} 
nav ul { 
    list-style:none; 
} 
nav li{ 
    margin-top: 5px; 
    border: 1px solid #000; 
} 
nav li a{ 
    display:block; 
    font-size: 0.8em; 
    text-decoration: none; 
    color: #aa0000; 
    padding-left: 15px; 
    background-color:#FFC; 
} 
nav li a:hover, #leftColumn li a:active, #leftColumn li a:focus{ 
    background-color: gray; 
    color: #ffffff; 
} 

aside { 
    -moz-border-radius: 5px; /* Ger rundade hörn i Firefox */ 
    border-radius: 5px; /* CSS3 */ 
    border: 1px solid #999; 
    padding: 4px; 
    margin-bottom:5px; 
} 

aside p { 
    font-size: 0.8em; 
} 

/* leftColumn */ 
#leftColumn { 
    float: left; 
    width: 180px; 
    margin: 8px; 
} 

#leftColumn h1 { 
    font-family:Arial, Helvetica, sans-serif; 
    font-size: 0.9em; 
} 
/* End leftColumn */ 
/* Main */ 
main { 
    margin-top: 8px; 
    margin-left:200px; 
} 
main h1{ 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 1.4em; 
} 
main h2{ 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 1.1em; 
} 
main section{ 
    float:right; 
    width: 99%; 
} 

form label,a{ 
    font-size: 0.8em; 
} 
/* End content */ 
/* Footer */ 
footer { 
    height: 30px; 
    background-image: url("../bilder/bgImg.png"); 
    color: white; 
    font-size: 0.75em; 
    clear:both; 
} 
footer #footerRight{ 
    float:right; 
    padding: 5px; 
} 
footer #footerLeft{ 
    float:left; 
    padding: 5px; 
} 
/* End footer */ 

模板的其餘部分是不是真的有必要對我來說,粘貼,因爲它是爲網站只是HTML代碼,我只是鏈接的CSS代碼如果你想檢查頁面看起來像你自己。如果您需要更多我可能忘記寫的信息,請通知我。

我知道我在start.php中使用include(「login.php」)存在問題,我只是不知道如何解決它,因爲我需要登錄表單中提交的信息。頁面控制器指出start.php是模板在該部分中使用的默認頁面。

編輯:對不起,我就不清楚了這一部分。它只會在我輸入正確的詳細信息時發生,例如admin:12345。

編輯2: 新start.php代碼

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

    if(isset($_SESSION['user'])){ 

    echo '<h1>Välkommen</h1>' . '<h1>' . $_SESSION['user'] . '</h1>'; 
    } 
?> 

回答

0

完整的login.php我使用:

<?php 
session_start(); 

if (isset($_POST['password'], $_POST['username'])) { 

    include("pwd.php"); 
    include("user.php"); 

    $key = $_POST['username']; 

    if (isset($user[$key])) { 
     $_SESSION['inloggad'] = true; // Användaren har anget rätt uppgifter. 
     $_SESSION['user'] = $_POST['username']; 
    } 


    if (isset($_SESSION['inloggad']) && ($_SESSION['inloggad'] === TRUE)) { 
     header("Location: index.php"); 
    } else { 
     echo "<h1>Vänligen logga in!</h1>"; 
    } 
} 
+0

與不真實的聲明它立即顯示重定向錯誤,因爲它只在我嘗試登錄後才顯示它。 – 97noan

+0

對不起誤解了代碼。 - 更新 – Wranorn

+0

感謝您的嘗試,但仍然得到重定向錯誤,一旦我按登錄。現在甚至出現這個錯誤:「一個會話已經開始 - 忽略第2行中的/ Applications/XAMPP/xamppfiles/htdocs/www/MOMENT 4/upp4/login.php中的session_start()」 – 97noan

0

index.php文件有一行default: include('start.php');可能導致此頁面重定向到本身,因爲文件start.php包括具有header("Location: index.php")文件login.php。同樣的問題將適用於該頁面上的任何其他包含文件導致包含header("Location: index.php")

+0

那你怎麼解決呢?因爲我想在登錄後直接在主頁面顯示用戶名。這就是爲什麼我需要發送到start.php的表單信息。 – 97noan

+0

剛剛設置爲'$ _SESSION ['user'] = $ _POST ['username'];在'index.php'中將用戶名作爲會話變量'$ _SESSION ['user']'在'login.php'中? –