-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
88 lines (77 loc) · 3.07 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Get the user email from the header provided by the IAP
$iap_user = $_SERVER['X-Goog-Authenticated-User-Email'];
$db_ini = parse_ini_file("db_conn.ini");
// Ensure the required environment variables are set to run the application
if (!$db_ini['CLOUDSQL_DSN'] || !$db_ini['CLOUDSQL_USER'] || false === $db_ini['CLOUDSQL_PASSWORD']) {
die('Set CLOUDSQL_DSN, CLOUDSQL_USER, and CLOUDSQL_PASSWORD in db_conn.ini file.');
}
# [START gae_cloudsql_example]
// If the unix socket is unavailable, try to connect using TCP. This will work
// if you're running a local MySQL server or using the Cloud SQL proxy, for example:
//
// $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
//
// This will mean your DSN for connecting locally to Cloud SQL would look like this:
//
// // for MySQL
// $dsn = "mysql:dbname=DATABASE;host=127.0.0.1";
// // for PostgreSQL
// $dsn = "pgsql:dbname=DATABASE;host=127.0.0.1";
//
$dsn = $db_ini['CLOUDSQL_DSN'];
$user = $db_ini['CLOUDSQL_USER'];
$password = $db_ini['CLOUDSQL_PASSWORD'];
// create the PDO client
$db = new PDO($dsn, $user, $password);
// create the tables if they don't exist
$sql = 'CREATE TABLE IF NOT EXISTS entries (guestName VARCHAR(255), content VARCHAR(255))';
$stmt = $db->prepare($sql);
$stmt->execute();
// Insert a new row into the guestbook on POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$stmt = $db->prepare('INSERT INTO entries (guestName, content) VALUES (:name, :content)');
$stmt->execute([
':name' => $_POST['name'],
':content' => $_POST['content'],
]);
}
// Query existing guestbook entries.
$results = $db->query('SELECT * from entries');
// Now you can use the PDOStatement object to print or iterate over the results:
//
// var_dump($results->fetchAll(PDO::FETCH_ASSOC));
# [END gae_cloudsql_example]
?>
<html>
<body>
<?php if ($results->rowCount() > 0): ?>
<h2>Guestbook Entries</h2>
<?php foreach ($results as $row): ?>
<div><strong> <?= $row['guestName'] ?></strong>: <?= $row['content'] ?></div>
<?php endforeach ?>
<?php endif ?>
<h2><font color="black">Sign the Guestbook</font></h2>
<form action="/" method="post">
<div>Name: <input name="name" value="<?= $iap_user ?>"/></div>
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<p>Build: [BULD_ID]</p>
</body>
</html>