Redirection avec header('location: ')
Bonsoir,
J'ai réalisé un mini-chat et j'ai un problème de redirection avec la fonction
Code:
header('Locate: ...');
J'ai 2 fichiers sources. Voici le fichier index.php...
Code:
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| <!DOCTYPE html>
<html lang="fr">
<head>
<title>Mini-Chat</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="index.css" />
</head>
<body>
<form method="post" action="minichat_post.php">
<?php
if(isset($_COOKIE['prenom']))
{
?>
<label for="prenom">Votre prenom : </label><input type="text" id="prenom" name="prenom" maxlength="11" value="<?php echo $_COOKIE['prenom'];?>" autofocus required /><br /><br />
<?php
}
else
{
?>
<label for="prenom">Votre prenom : </label><input type="text" id="prenom" name="prenom" maxlength="11" placeholder="Ex : Roxi" autofocus required /><br /><br />
<?php
}
?>
<textarea name="message" cols="27" rows="5" required>Votre message...</textarea><br /><br />
<input type="submit" value="Envoyer" />
</form>
<?php
try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=10.111.145.45;dbname=marvin11300-test', 'marvin11300', 'vN1aYUXMVc', $pdo_options);
if(isset($_GET['page']))
{
if($_GET['page'] == 'Page 1')
{
$selected1 = 'selected';
$selected2 = '';
$selected3 = '';
$selected4 = '';
$req = $bdd->query('SELECT * FROM minichat ORDER BY id DESC LIMIT 0,10');
}
else if($_GET['page'] == 'Page 2')
{
$selected2 = 'selected';
$selected1 = '';
$selected3 = '';
$selected4 = '';
$req = $bdd->query('SELECT * FROM minichat ORDER BY id DESC LIMIT 10,10');
}
else if($_GET['page'] == 'Page 3')
{
$selected3 = 'selected';
$selected2 = '';
$selected1 = '';
$selected4 = '';
$req = $bdd->query('SELECT * FROM minichat ORDER BY id DESC LIMIT 20,10');
}
else
{
$selected4 = 'selected';
$selected2 = '';
$selected3 = '';
$selected1 = '';
$req = $bdd->query('SELECT * FROM minichat ORDER BY id DESC LIMIT 30,10');
}
}
else
{
$selected1 = 'selected';
$selected2 = '';
$selected3 = '';
$selected4 = '';
$req = $bdd->query('SELECT * FROM minichat ORDER BY id DESC LIMIT 0,10');
}
?>
<p>
<?php
while($donnees = $req->fetch())
{
echo '<strong>' . strip_tags($donnees['prenom']) . '</strong> : ' . strip_tags($donnees['message']) . '<br />';
}
?>
</p>
<form method="get" action="index.php">
<select name="page">
<option value="Page 1" <?php echo $selected1; ?>>Page 1</option>
<option value="Page 2" <?php echo $selected2; ?>>Page 2</option>
<option value="Page 3" <?php echo $selected3; ?>>Page 3</option>
<option value="Page 4" <?php echo $selected4; ?>>Page 4</option>
</select>
<input type="submit" />
</form>
<?php
$req->closeCursor();
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
?>
</body>
</html> |
et le fichier minichat_post.php...
Code:
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
| <?php
setcookie('prenom', $_POST['prenom'], time() + 365 * 24 * 3600, null, null, false, true);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<title>Mini-Chat</title>
<meta charset="utf-8" />
</head>
<body>
<?php
try
{
if(isset($_POST['message']) AND isset($_POST['prenom']) AND $_POST['message'] != 'Votre message...')
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=10.111.145.45;dbname=marvin11300-test', 'marvin11300', 'vN1aYUXMVc', $pdo_options);
$req = $bdd->prepare('INSERT INTO minichat(prenom, message) VALUES(:prenom, :message)');
$req->execute(array(
'prenom' => $_POST['prenom'],
'message' => $_POST['message']));
header('Location: index.php');
}
else
{
header('Location: index.php');
}
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
?>
</body>
</html> |
En faisant des recherche, j'ai appris qu'il ne faut rien mettre avant la redirection. Je ne comprend pas comment il ne peux rien avoir avant la redirection. Pouvez vous svp me proposer une solution en m'expliquant pourquoi ce que j'ai fais ne fonctionne pas.