Advertisement
Guest User

Untitled

a guest
Apr 5th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2.  
  3. function h($str) {
  4.     return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  5. }
  6.  
  7. $p = new stdClass;
  8. foreach (array('info', 'password', 'persistent', 'submit') as $v) {
  9.     $p->$v = isset($_POST[$v]) && is_string($_POST[$v]) ? $_POST[$v] : '';
  10. }
  11. $c = new stdClass;
  12. foreach (array('password', 'persistent') as $v) {
  13.     $c->$v = isset($_COOKIE[$v]) && is_string($_COOKIE[$v]) ? $_COOKIE[$v] : '';
  14. }
  15.  
  16. $info = is_file('info.txt') ? file_get_contents('info.txt') : '';
  17. $password = $c->password;
  18. $persistent = $c->persistent ? ' checked' : '';
  19.  
  20. if ($p->submit) {
  21.     $info = $p->info;
  22.     $password = $p->password;
  23.     $persistent = $p->persistent ? ' checked' : '';
  24.     $expire = $persistent ? time() + 604800 : 1;
  25.     setcookie('persistent', '1', $expire);
  26.     setcookie('password', $password, $expire);
  27.     if ($p->password !== 'XXXXXXXXXXXXXXXX') {
  28.         $msg = array('red', 'パスワードが違います');
  29.     } else {
  30.         file_put_contents('info.txt', $p->info, LOCK_EX);
  31.         $msg = array('green', 'ファイルを更新しました');
  32.     }
  33. }
  34.  
  35. header('Content-Type: text/html; charset=utf-8');
  36.  
  37. ?>
  38. <!DOCTYPE html>
  39. <html>
  40. <head>
  41.   <title>新着情報更新</title>
  42. </head>
  43. <body>
  44.   <h1>新着情報</h1>
  45.   <form method="post" action="">
  46. <?php if (isset($msg)): ?>
  47.     <p><span style="color:<?=h($msg[0])?>;"><?=h($msg[1])?></span></p>
  48. <?php endif; ?>
  49.     <p>
  50.       内容<br />
  51.       <textarea name="info" cols="50" rows="7"><?=h($info)?></textarea>
  52.     </p>
  53.     <p>
  54.       パスワード
  55.       <input type="password" name="password" value="<?=h($password)?>"><br />
  56.       <input type="checkbox" name="persistent" value="1"<?=h($persistent)?>>パスワードを保存する
  57.     </p>
  58.     <input type="submit" name="submit" value="更新">
  59.   </form>
  60. </body>
  61. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement