2016-01-13 78 views
1

我正在設計一個帶有登錄頁面的網站。登錄部分和從數據庫抓取工作正常。但我想在登錄後在主頁上顯示用戶名。我使用session_start()。但用戶名不顯示。我在這裏查了類似的問題,但沒有一個是我的答案。以下是代碼: PHP代碼:在網站上登錄後顯示用戶名

$email = mysqli_real_escape_string($con,$_POST['user']); 
$pass = mysqli_real_escape_string($con,$_POST['pass']); 
$sel_user = "select * from admins where email='$email' AND password='$pass'"; 
$run_user = mysqli_query($con, $sel_user); 
$check_user = mysqli_num_rows($run_user); 
if($check_user>0) 
{ 
$_SESSION['email']=$email 
echo "<script>window.open('index2.html','_self')</script>"; 
} 
else { 
echo "<script>alert('Email or password is not correct, try again!')</script>"; 
} 

index2.html:

<?php 
     session_start(); 
?> 
    <div class="inline-block"> 
           <h5 class="no-margin"> Welcome </h5> 
           <h4 class="no-margin"> <?php echo $_SESSION['email']; ?> </h4> 
           <a class="btn user-options sb_toggle"> 
            <i class="fa fa-cog"></i> 
           </a> 
          </div> 
+1

你必須在你的php腳本的每個文件中包括'start_session()',包括你在哪裏設置'$ _SESSION ['email']' – noor

+1

你在html文件中使用php標籤。將index_2.html更改爲index_2.php ...完成 –

+0

您必須在第一個文件中找到的$ _SESSION ['email'] = $ email末尾放置分號。 –

回答

0

嘗試以下代碼:

第一代碼

session_start(); 
$email = mysqli_real_escape_string($con,$_POST['user']); 
$pass = mysqli_real_escape_string($con,$_POST['pass']); 
$sel_user = "select * from admins where email='$email' AND password='$pass'"; 
$run_user = mysqli_query($con, $sel_user); 
$check_user = mysqli_num_rows($run_user); 
if($check_user>0) 
{ 
    $_SESSION['email']=$email 
    echo "<script>window.open('index2.html','_self')</script>"; 
} 
else { 
    echo "<script>alert('Email or password is not correct, try again!')</script>"; 
} 

index2.php

<?php session_start(); ?> 
<div class="inline-block"> 
    <h5 class="no-margin"> Welcome </h5> 
    <h4 class="no-margin"> <?php echo $_SESSION['email']; ?> </h4> 
    <a class="btn user-options sb_toggle"> 
     <i class="fa fa-cog"></i> 
    </a> 
</div> 

它應該工作。

+0

我已經在我的htaccess中添加了AddType application/x-httpd-php5 .html .htm。所以我可以在我的html中運行php.but當你的代碼提到我應該添加一個會話開始也在我的PHP代碼。所以它現在正在工作。 thhks – user3696174

+0

歡迎,很高興爲您效勞! –

+0

*「有一個很大的錯誤,就是你不能在.html文件中運行PHP。」 - 這是錯誤的,應該從答案中丟棄,或修改爲讀取如下內容:*「你需要指示Apache來處理.html文件作爲PHP「*,就像OP說的那樣。 –

相關問題