2016-05-20 32 views
0

我有一個網頁,只允許用戶上傳圖像,如果他已經登錄,我使用session_start()來獲取變量$ _SESSION ['username']。如何設置我的文本圖像字段,使其只有在$ _SESSION ['username']的設置值有效時纔可見。如果未設置變量,則禁用輸入字段

我以下代碼:

<form action="uploadtofile.php" method="post" enctype="multipart/form- data"> 
<input id="bc0" type="file" name="fileToUpload" id="fileToUpload" > 
<input id="bc1" type="submit" value="Upload Image" name="submit" > 
</form> 
session_start(); 
if(isset{$_SESSION['username']{ 

.bc01.visibility =「隱藏}

回答

1

用下面的代碼,如果用戶登錄的form將僅顯示

<?php 
session_start(); 
if(isset($_SESSION['username'])) { 
echo' 
<form action="uploadtofile.php" method="post" enctype="multipart/form-data"> 
<input id="bc0" type="file" name="fileToUpload" id="fileToUpload"> 
<input id="bc1" type="submit" value="Upload Image" name="submit"> 
</form> 
'; 
} 
?> 
+0

謝謝你的答案像一個魅力,如果用戶未登錄的領域是隱藏的! –

+0

@ AbsarA.Khan _No problemo! :)_ –

+0

還有一個問題,如果你不介意:) 我想呼應用戶名(通過我的會話變量傳遞和回聲「你好(用戶名)」如果我登錄或說「你好客人」如果IM沒有登錄英寸u能告訴我做什麼我錯了,請 '如果<?PHP \t \t \t \t \t \t \t(isset($ _ SESSION [' 名'])){ \t \t \t \t \t \t \t \t \t回聲「 WELCOME'。$ _ SESSION ['username')。' 「; \t \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t \t別的 \t \t \t \t \t \t \t \t \t \t回波 '歡迎訪客'; \t \t \t \t \t \t \t \t \t?>「 –

0

很高興看到您創建了一個 session_start(); 很多人都有問題記得這麼做!

然後,您可以做如下,並創建一個如果被觸發的語句時,他們的細節是正確的:

if(check the db details and compare to see if logged in) 
{ 
    $_SESSION['logCheck'] = true; 
    $_SESSION['username'] = $username; 
    $_SESSION['userid'] = $userid; 
} 

然後創建一個功能,如果你想使用它幾次,檢查他們已登錄:

if (isset($_SESSION['logCheck']) && $_SESSION['logCheck'] == true) { 
    echo "Logged in" . $_SESSION['username']; 
} else { 
    echo "Not logged in"; 
} 
相關問題