2012-01-10 10 views
0

この単純なスクリプトはデータベースを必要とせず、ユーザー名とパスワードがスクリプトで定義されます。
どのようにして、各ユーザ名とパスワードの日付を定義して、期限が切れた後にその資格情報を使用してログインする機能も期限切れになるようにスクリプトを作成できますか?このようPHPスクリプトでログインするための制限日を作成する

$LOGIN_INFORMATION = array( 
'you' => 'yourpassword' => '12-03-2012' 
'username2' => 'yourpassword2' => '12-05-2012' 
); 

スクリプトデモ:http://demos.savasplace.com/password/

<?php 
################################################################## 
##   Sava's Place.com Password Protect     ## 
##  Visit http://savasplace.com for more scripts   ## 
################################################################## 
$LOGIN_INFORMATION = array(
    'sava' => 'sava', 
    'you' => 'yourpassword' 
); 

// Require username or not 
// If you want to use username and password login leave this true. 
// If you want only to request a password set it for false. 

define('USE_USERNAME', true); 

// To password protect your pages you need to include this file in them 
// To get the correct code for inclusion open password.php?code in your broswer 

if(isset($_GET['code'])) { 
    die('<center><font face="Verdana" size="2">Include following code into every page you would like to protect, at the very beginning (first line):<br><br><strong>&lt;?php include("' . __FILE__ . '"); ?&gt;</strong></center></font>'); 
} 
if(isset($_GET['logout'])) { 
    setcookie("verify", ''); // clear password; 
    die('<center><font face="Verdana" size="2">Logged out.</font></center>'); 
} 
if(!function_exists('showLoginPasswordProtect')) { 
function showLoginPasswordProtect($error_msg) { 
?> 
<!-- And this is the output --> 

<html> 
<head> 
<title>Login to access this page</title> 
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> 
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> 
<style> 
* { 
font-family:Verdana; 
font-size:12px; 
} 
a { 
color: #000000; 
text-decoration: none; 
} 

a:hover { 
color: red; 
text-decoration: none; 
} 

input { 
border: 1px solid black; 
background-color: #FFFFFF; 
} 
.bodyform { 
border-color: #000000; 
border-style:solid; 
border-width: 3px; 
width:400px; 
} 
.title { 
color: #FFFFFF; 
background: #000000; 
padding: 5px; 

} 
</style> 
</head> 
<body> 
<center><br /><br /> 
<div class="bodyform" align="center"> 
<form method="post"> 
<div class="title"> 
<strong>Access restricted - Login Below:</strong> 
</div> 
<div style="padding-right:10px;padding-left:10px;"> 
<font color="red"><strong><?php echo $error_msg; ?><strong></font><br /><br /> 
<?php if (USE_USERNAME) echo '<table> 
<tr> 
<td align="left">Login:</td> 
<td align="right"><input type="input" name="access_login" /></td> 
</tr> 
<tr> 
<td>Password:</td>'; ?> 
<td><input type="password" name="access_password" /></td> 
</tr> 
</table> 
<p></p> 
<input type="submit" name="Submit" value="Submit" /> 
</form> 
</div> 
</div> 
<br /> 
Powered by <a href="http://savasplace.com" target="_blank">Sava's Place Password Protect Script</a> 
</center> 
</body> 
</html> 
<?php 
    die(); 
} 
} 
if (isset($_POST['access_password'])) { 
    $login = isset($_POST['access_login']) ? $_POST['access_login'] : ''; 
    $pass = $_POST['access_password']; 
    if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION) 
    || (USE_USERNAME && (!array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass)) 
) { 
    showLoginPasswordProtect("Incorrect password."); 
    } 
    else { 
    setcookie("verify", md5($pass)); 
    unset($_POST['access_login']); 
    unset($_POST['access_password']); 
    unset($_POST['Submit']); 
    } 

} 
else { 
    if (!isset($_COOKIE['verify'])) { 
    showLoginPasswordProtect(""); 
    } 
    $found = false; 
    foreach($LOGIN_INFORMATION as $kay=>$val) { 
    if ($_COOKIE['verify'] == md5($val)) { 
     $found = true; 
     break; 
    } 
    } 
    if (!$found) { 
    showLoginPasswordProtect(""); 
    } 
} 
?> 
+4

」タグ?真剣に?彼らは**年**非難されている! – ThiefMaster

答えて

0

あなたはまたに変換され、現在のデータを時間数値にあなたの日付を変換し、あなたにそれを比較することstrtotimeを使用することができます数値であり、指定した値より大きい場合はログイン時にfalseを返します。

$LOGIN_INFORMATION = array( 
    'you' => array('pass' => 'yourpassword', 'date' => '12-03-2012'), 
    'username2' => array('pass' => 'yourpassword2', 'date' => '12-05-2012') 
); 


if(strtotime($LOGIN_INFORMATION['you']['date']) > time()) 
{ 
    exit(); 
    // show error message or anything 
} 
else 
{ 
    // login or whatever you would like to happen here in case the date did not expired 
    // check password, etc 
} 

php function strtotime

php function time

+0

コマンド**をこのスクリプトに含めるにはどうすれば** strtotimeを含めることができますか? このコマンドを入力する必要があるのはどの部分ですか? – Amirreza

+0

@Amirrezaあなたが冒頭であなたの.phpファイルに上記のコードサンプルを書いているだけです。おそらく '$ pass = $ _POST ['access_password']の後に置くのが最も良いでしょう。 'が、何を知っていますか?たぶんあなたは少なくともこれをやって、他の何かがあなたを困惑させるようなことをやろうとします。 – khael

+0

どのように私はこのコードを編集してメインスクリプトに入れることができますか? ** if(strtotime($ LOGIN_INFORMATION ['あなた'] ['日付'])>時間())** – Amirreza

0

あなたはへsetcookieを変えることができる:

setcookie("verify", md5($pass), time()+3600);

、その後クッキーは1時間後に期限切れになり、ユーザーはもはやログインしないであろうあなたがクッキー/ログインを長くしたいなら、あなたはexpを変更するでしょうireパラメータを長い時間に設定します。

関連する問題