リロード禁止にしたいですが、うまく行かないです。
昔メール二重送信を禁止するときに使ってたコードを使ってますが、うまく行きませんでした。
やりたいことは、info.phpがブラウザで表示したときにf5を押してもリロードしないようにしたいです。
回答よろしくお願いいたします。
index.php
php
1<?php 2session_start(); 3$_SESSION['ticket']=SHA1(uniqid().mt_rand()); 4?> 5<html> 6<head> 7 8<title>DB</title> 9<link rel="stylesheet" type="text/css" href="css.css"> 10</head> 11<body> 12 13<form method="post" name="form" action="insert.php" > 14<input type="text" name="name"> 15<input type="submit"> 16</form> 17 18<table border="2" cellspacing="10" width="200px"> 19 <tr> 20 <th scope="col">ID</th> 21 <th scope="col">入力値</th> 22 <th scope="col">詳細</th> 23 </tr> 24 25<?php 26 27if(isset($_GET['error'])) { 28 $error = $_GET['error']; 29 if(strpos($error, '1') !== FALSE) { 30 echo '<span style="color: #ee0000">入力して下さい!</span>'; 31 } 32} 33 34$link = mysql_connect('localhost', 'root', ''); 35if (!$link) { 36 die('接続失敗です。'.mysql_error()); 37} 38 39 40 41$db_selected = mysql_select_db('kk', $link); 42if (!$db_selected){ 43 die('データベース選択失敗です。'.mysql_error()); 44} 45 46 47 48mysql_set_charset('utf8'); 49 50$result = mysql_query('SELECT id,name FROM user'); 51if (!$result) { 52 die('クエリーが失敗しました。'.mysql_error()); 53} 54 55 56 57while ($row = mysql_fetch_assoc($result)) { 58 ?> 59 60 <tr> 61 <td><?php print(htmlspecialchars($row['id']));?></td> 62 <td><?php print(htmlspecialchars($row['name']));?></td> 63 <td id="ss"><a href="info.php?id=<?php print $row['id']; ?>">詳細</a></td> 64</tr> 65 66<?php 67} 68$close_flag = mysql_close($link); 69 70?> 71</table> 72</body> 73</html>
insert.php
php
1<?php 2session_start(); 3 4 5$data = $_POST['name']; 6if($data === "") { 7$error = "1"; 8 header('Location:http://localhost/db2/index.php?error='.$error); 9}else{ 10 11$link = mysql_connect('localhost', 'root', ''); 12if (!$link) { 13 die('接続失敗です。'.mysql_error()); 14} 15 16print('<p>接続に成功しました。</p>'); 17 18$db_selected = mysql_select_db('kk', $link); 19if (!$db_selected){ 20 die('データベース選択失敗です。'.mysql_error()); 21} 22 23print('<p>kkデータベースを選択しました。</p>'); 24 25// MySQLに対する処理 26 27mysql_set_charset('utf8'); 28 $data = mysql_real_escape_string($data); 29 $sql = "INSERT INTO user(name,ins_date) VALUES ('$data',now())"; 30 31 $result_reg = mysql_query($sql); 32 if(!$result_reg){ 33 echo "SQL失敗<br />"; 34 exit; 35 }else{ 36 echo "SQL成功<br />"; 37 } 38$close_flag = mysql_close($link); 39 40if ($close_flag){ 41 print('<p>切断に成功しました。</p>'); 42 header('Location:http://localhost/db2/index.php'); 43} 44} 45 46?>
info.php
php
1<?php 2 session_start(); 3if (isset($_POST['ss'],$_SESSION['ticket']) && $_SESSION['ticket']==$_POST['ticket']) { 4 unset($_SESSION['ticket']); 5}else{ 6 header('Location:index.php'); 7 exit(); 8} 9 10?> 11<html> 12<head> 13 14<title>DB</title> 15 16<link rel="stylesheet" type="text/css" href="css.css"> 17</head> 18<body> 19 20 21 <?php 22 23$link = mysql_connect('localhost', 'root', ''); 24if (!$link) { 25 die('接続失敗です。'.mysql_error()); 26} 27 28 29 30$db_selected = mysql_select_db('kk', $link); 31if (!$db_selected){ 32 die('データベース選択失敗です。'.mysql_error()); 33} 34 35mysql_set_charset('utf8'); 36 37?> 38 39<?php 40$id = $_GET['id']; 41$result = mysql_query("SELECT * FROM user WHERE id='$id'"); 42mysql_real_escape_string($id); 43 44if (!$result) { 45 die('クエリーが失敗しました。'.mysql_error()); 46} 47 48while ($row = mysql_fetch_assoc($result)) { 49 50 51 ?> 52<table border="1" cellspacing="10" width="200px"> 53 <tr> 54 <th>日付</th> 55 <td><?php 56 print date('Y年n月j日 H時i分s秒', strtotime($row['ins_date']));?></td> 57 </tr> 58 <tr> 59 <th>ID</th> 60 <td><?php print(htmlspecialchars($row['id']));?></td> 61 </tr> 62 <tr> 63 <th>入力値</th> 64 <td><?php print(htmlspecialchars($row['name']));?></td> 65 </tr> 66 </table> 67 68 69<?php 70} 71mysql_free_result($result); 72$close_flag = mysql_close($link); 73 74?> 75<a href="index.php"><input type="submit" value="トップページへ" style="margin-top:20px; margin-left:80px"></a> 76</body> 77</html>
回答2件
あなたの回答
tips
プレビュー