I'm currently following the build-a-blog tutorial, and have got as far as the password protected section. I was going to skip it but skipping it caused problems when I went on to the 'edit an entry' section.
But anyway, as you can see the index page shows up fine, but once I enter the username (which I kept as USERNAME) and password (kept that as PASSWORD too), I get a 404 error:
http://retro-bella.net/blog/index.php
I'm totally new to this so I can't figure out what's wrong.
Here my coding for my index page:
CODE
<?php
mysql_connect ('localhost', 'rhiface1_blog', '_______');
mysql_select_db ('rhiface1_blog');
$sql = "SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 5";
$result = mysql_query($sql) or print ("Can't select entries from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['timestamp']);
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
$password = $row['password'];
$id = $row['id'];
if ($password == 1) {
echo "<p><strong>" . $title . "</strong></p>";
printf("This is a password protected entry. If you have a password, log in below.");
printf("<form method=\"post\" action=\"journal.php?id=%s\"><p><strong><label for=\"username\">Username:</label></strong><br /><input type=\"text\" name=\"username\" id=\"username\" /></p><p><strong><label for=\"pass\">Password:</label></strong><br /><input type=\"password\" name=\"pass\" id=\"pass\" /></p><p><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"submit\" /></p></form>",$id);
print "<hr />";
}
else { ?>
<p><strong><?php echo $title; ?></strong><br /><br />
<?php echo $entry; ?><br /><br />
Posted on <?php echo $date; ?>
<hr /></p>
<?php
}
}
?>
Heres my single page entry code:
CODE
<?php
mysql_connect ('localhost', 'rhiface1_blog', '_______');
mysql_select_db ('rhiface1_blog');
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid ID specified.");
}
$my_username = "USERNAME";
$my_password = "PASSWORD";
$id = (int)$_GET['id'];
$sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";
$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y", $row['timestamp']);
$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
$password = $row['password'];
if ($password == 1) {
if (isset($_POST['username']) && $_POST['username'] == $my_username) {
if (isset($_POST['pass']) && $_POST['pass'] == $my_password) {
?>
<p><strong><?php echo $title; ?></strong><br /><br />
<?php echo $entry; ?><br /><br />
Posted on <?php echo $date; ?>
<hr /></p>
<?php
}
else { ?>
<p>Sorry, wrong password.</p>
<?php
}
}
else {
echo "<p><strong>" . $title . "</strong></p>";
printf("<p>This is a password protected entry. If you have a password, log in below.</p>");
printf("<form method=\"post\" action=\"journal.php?id=%s\"><p><strong><label for=\"username\">Username:</label></strong><br /><input type=\"text\" name=\"username\" id=\"username\" /></p><p><strong><label for=\"pass\">Password:</label></strong><br /><input type=\"password\" name=\"pass\" id=\"pass\" /></p><p><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"submit\" /></p></form>",$id);
print "<hr /><br /><br />";
}
}
else { ?>
<p><strong><?php echo $title; ?></strong><br /><br />
<?php echo $entry; ?><br /><br />
Posted on <?php echo $date; ?></p>
<?php
}
}
?>
And here's my 'new entry' code:
CODE
<?php
if (isset($_POST['submit'])) {
$month = htmlspecialchars(strip_tags($_POST['month']));
$date = htmlspecialchars(strip_tags($_POST['date']));
$year = htmlspecialchars(strip_tags($_POST['year']));
$time = htmlspecialchars(strip_tags($_POST['time']));
$title = htmlspecialchars(strip_tags($_POST['title']));
$entry = $_POST['entry'];
$password = htmlspecialchars(strip_tags($_POST['password']));
$timestamp = strtotime($month . " " . $date . " " . $year . " " . $time);
$entry = nl2br($entry);
if (!get_magic_quotes_gpc()) {
$title = addslashes($title);
$entry = addslashes($entry);
}
mysql_connect ('localhost', 'rhiface1_blog', '_______');
mysql_select_db ('rhiface1_blog');
$sql = "INSERT INTO php_blog (timestamp,title,entry,password) VALUES ('$timestamp','$title','$entry','$password')";
$result = mysql_query($sql) or print ("Can't insert into table php_blog.<br />" . $sql . "<br />" . mysql_error());
if ($result != false) {
print "Your entry has successfully been entered into the database.";
}
mysql_close();
}
?>
<?php
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><strong><label for="month">Date (month, day, year):</label></strong>
<select name="month" id="month">
<option value="<?php echo $current_month; ?>"><?php echo $current_month; ?></option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<input type="text" name="date" id="date" size="2" value="<?php echo $current_date; ?>" />
<select name="year" "year">
<option value="<?php echo $current_year; ?>"><? echo $current_year; ?></option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2008</option>
<option value="2010">2010</option>
</select>
<strong><label for="time">Time:</label></strong> <input type="text" name="time" id="time" size="5" value="<?php echo $current_time; ?>" /></p>
<p><strong><label for="title">Title:</label></strong> <input type="text" name="title" name="title" size="40" /></p>
<p><strong><label for="password">Password protect?</label></strong> <input type="checkbox" name="password" id="password" value="1" /></p>
<p><textarea cols="80" rows="20" name="entry" id="entry"></textarea></p>
<p><input type="submit" name="submit" id="submit" value="Submit"></p>
</form>