Dovrei realizzare uno script in php che mi permetta di pubblicare dei listini in formato pdf. Devo poter usare una password per aggiungere i listini e una solo per accedere alla pagina dei pdf oppure posso anche solo accontentarmi della prima e sostituire la seconda con una inclusa nel documento pdf.
Qui sotto trovare una bozza di uno script che secondo me calza a meraviglia. Il programma che vedete aggiunge dei joke (stringhe di testo) usando un form apposito in una pagina chiamata index.php. A me basterebbe sostituire queste stringhe di testo con dei link su cui cliccare per fare il download dei pdf. Sto leggendo un manuale in pdf e sono solo all'inizio così vorrei sapere se potevate darmi una mano a modificare questo codice.
error.html.php
Codice PHP:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script Error</title>
</head>
<body>
<p>
<?php echo $error; ?>
</p>
</body>
</html>
form.html.php
Codice PHP:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add Joke</title>
<style type="text/css">
textarea {
display: block;
width: 100%;
}
</style>
</head>
<body>
<form action="?" method="post">
<div>
<label for="joketext">Type your joke here:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40"></textarea>
</div>
<div><input type="submit" value="Add"></div>
</form>
</body>
</html>
index.php
Codice PHP:
<?php
if (get_magic_quotes_gpc())
{
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process))
{
foreach ($val as $k => $v)
{
unset($process[$key][$k]);
if (is_array($v))
{
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
}
else
{
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
if (isset($_GET['addjoke']))
{
include 'form.html.php';
exit();
}
try
{
$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'mypassword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$error = 'Unable to connect to the database server.';
include 'error.html.php';
exit();
}
if (isset($_POST['joketext']))
{
try
{
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted joke: ' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
if (isset($_GET['deletejoke']))
{
try
{
$sql = 'DELETE FROM joke WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting joke: ' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
try
{
$sql = 'SELECT id, joketext FROM joke';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching jokes: ' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $result->fetch())
{
$jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
}
include 'jokes.html.php';
jokes.html.php
Codice PHP:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List of Jokes</title>
</head>
<body>
<p><a href="?addjoke">Add your own joke</a></p>
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $joke): ?>
<form action="?deletejoke" method="post">
<blockquote>
<p>
<?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8'); ?>
<input type="hidden" name="id" value="<?php echo $joke['id']; ?>">
<input type="submit" value="Delete">
</p>
</blockquote>
</form>
<?php endforeach; ?>
</body>
</html>