Created
April 3, 2018 02:45
-
-
Save noriyukitakei/8944b2a09cadd1cd3beda8ef6b4d967b to your computer and use it in GitHub Desktop.
OAuthプロバイダー(Facebook側)の投稿一覧画面(messages.php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// セッションを開始します。 | |
session_start(); | |
// usernameというセッション変数に何もセットされていなかったら、 | |
// すなわち未認証だったら、ログイン画面にリダイレクトします。 | |
if (!isset($_SESSION['username'])) { | |
// ログイン画面にリダイレクトする前に、ログイン後に | |
// 戻る画面の戻り先のURL(つまり本画面のURL)をセッションに格納します。 | |
$_SESSION['back'] = $_SERVER["REQUEST_URI"]; | |
header('Location: /login.php'); | |
exit; | |
} | |
// データベースの接続に必要なPDOインスタンスを作成します。 | |
$pdo = new PDO('mysql:dbname=provider;host=localhost', 'root', 'password'); | |
// メッセージが投稿されたらデータベースに反映します。 | |
if (!empty($_POST["post"])) { | |
$stmt = $pdo->prepare('INSERT INTO messages VALUES(:username,:message)'); | |
$stmt->execute(array(':message' => $_POST["message"], ':username' => $_SESSION['username'])); | |
} | |
// メーッセージの一覧を取得します。 | |
$stmt2 = $pdo->prepare('SELECT * FROM messages'); | |
$stmt2->execute(); | |
?> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Facebook投稿一覧画面</title> | |
</head> | |
<body> | |
<h1>Facebook投稿一覧画面</h1> | |
<form action="/messages.php" method="POST"> | |
<input type="text" name="message" value=""> | |
<input type="submit" name="post" value="投稿"> | |
</form> | |
<table border="1"> | |
<tr> | |
<td>ユーザー名</td><td>メッセージ</td> | |
</tr> | |
<?php | |
while($result = $stmt2->fetch(PDO::FETCH_ASSOC)){ | |
echo("<tr>"); | |
echo("<td>".$result['username']."</td>"); | |
echo("<td>".$result['message']."</td>"); | |
echo("</tr>"); | |
} | |
?> | |
</table> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment