ログイン+ニュースフィードのサイトを作っています。必要なすべてのデータベースをセットアップしました。
これは、すべてのサイトのコードです。
Index.php:
<?php
session_start();
mysql_pconnect("MyServer","Username","Password");
mysql_select_db("Database_Name");
$page = $_GET['page'];
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>InstaWord</title>
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<center>
<table>
<tr>
<td id="body">
<h1>News Feed <a href="?page=Home">Home</a></h1>
<?php
if ( !$page ) {
$page = "Home";
}
include("$pages.php");
?>
</td>
<td id="nav">
<?php
if ( !$_SESSION['uid'] ) {
?> <a href="?page=Login">Login</a>
<?php
} else {
?>
<a href="?page=Admin">Profile</a> | <a href="?page=Logout">Logout</a>
<?php
}
?>
</td>
</tr>
</table>
</center>
</body>
</html>
style.css は次のようになります。
body {
background: #cfcfcf;
font-size: 12px;
line-height: 18px;
font-family: Trebuchet MS;
}
td {
font-size: 12px;
line-height: 18px;
font-family: Trebuchet MS;
}
#body {
background: #FFF;
border: 1px solid #000;
width: 600px;
padding: 10px;
}
#nav {
background: #FFF;
border: 1px solid #000;
padding: 10px;
width: 125px;
}
Home.php は次のようになります。
<?php
$sql = mysql_query("SELECT * FROM `news1`") or die(mysql_error());
if ( mysql_num_rows($sql) == 0 ) {
echo "There are no posts.";
} else {
$sql = mysql_query("SELECT * FROM `news1` ORDER BY `time` DESC") or die(mysql_error());
while ( $row = mysql_fetch_assoc($sql) ) {
echo "<hr>";
echo "<h3>";
echo $row['news_title'];
echo "</h3>";
echo $row['news_body'];
}
}
?>
Login.php は次のようになります。
<?php
if ( !$_SESSION['uid'] ) {
if ( !$_POST['submit'] ) {
?>
<form action="?page/Login" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<td colspan="2">
<input type="submit" value="Login" name="submut">
</td>
</tr>
</table>
</form>
<?php
} else {
$username = $_POST['username'];
$password = md5($_POST['password']);
$errors = array();
if ( !$username ) {
$errors[1] = "Please enter a username.";
}
if ( !$password ) {
$errors[2] = "Please enter a password";
}
$sql = "SELECT * FROM `users3` WHERE `name`='$username'";
$res = mysql_query($sql) or die(mysql_error());
$exists = mysql_num_rows($res);
if ( $exists == 0 ) {
$errors[3] = "The username does not exist!";
} else {
$sql = "SELECT * FROM `users3` WHERE `name`='$username' AND `password`='$password'";
$res = mysql_query($sql) or die(mysql_error());
$exists = mysql_num_rows($res);
if ( $exists == 0 ) {
$errors[4] = "The username and password do not match.";
}
}
if ( count($errors) > 0 ) {
echo "<ul>";
foreach ( $errors as $error ) {
echo "<li>";
echo $error;
echo "</li>";
}
echo "</ul>
} else {
$sql = "SELECT * FROM `users3` WHERE `name`='$username' AND `password`='$password'";
$res = mysql_query($sql) or die(mysql_error());
$res = mysql_fetch_assoc($res);
$_SESSION['uid'] = $res['user_id'];
echo "You have successfully logged in! <a href=\"? page=Home\">Home</a><br><br>";
}
} else {
echo "You are already logged in!";
include("?pages/Home.php");
}
}
}
?>
Logout.php は次のようになります。
<?php
$_SESSION['uid'] = "";
?>
You have logged out!
Admin.php は次のようになります。
<?php
$action = $_GET['action'];
if ( !$action ) {
?>
<a href="?page=Admin&action=Post">Post News</a><br>
<a href="?page=Admin&action=Edit">Edit/Delete News</a><br>
<?php
} else {
if ( $action == "Post" ) {
if ( !$_POST['submit'] ) {
?>
<form action="?page=Admin&action=Post" method="post">
<table>
<tr>
<td>News Title</td>
<td><input type="text" name="news_title"></td>
</tr>
<tr>
<td>News Body</td>
<td><textarea name="news_body" cols="8" rows="4"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Post News">
</td>
</tr>
</table>
</form>
<?php
} else {
$user_id = $_SESSION['uid'];
$news_title = $_POST['news_title'];
$news_body = $_POST['news_body'];
$time = time();
$errors = array();
if ( !$user_id ) {
$errors[1] = "You are not logged in.";
}
if ( !$news_title ) {
$errors[2] = "Please enter a title of the news.";
}
if ( !$news_body ) {
$errors[3] = "Please enter a news body.";
}
if ( count($errors) > 0 ) {
echo "<ul>";
foreach ( $errors as $error ) {
echo "<li>";
echo $error;
echo "</li>";
}
echo "</ul>";
} else {
$sql = mysql_query("INSERT INTO `news1` (`news_id`, `news_title`, `news_body`, `user_id`, `time`)
VALUES ('$news_title', '$news_body', '$user_id', '$time');';") or die(mysql_error());
echo "You have posted the news successfully!";
}
}
} elseif ( $action == "Edit" ) {
if ( !$_GET['id'] ) {
$sql = mysql_query("SELECT * FROM `news1`") or die(mysql_error());
while ( $row = mysql_fetch_assoc($sql) ) {
echo $row['news_title'] . " | <a href=\"?page=Admin&action=Edit&id=" . $row['news_id'] . "&type=Delete\">Delete</a><br>";
}
} else {
$type = $_GET['type'];
$id = $_GET['id'];
if ( $type == "Delete" ) {
$sql = mysql_query("DELETE FROM `news1` WHERE `news_id`='$id'") or die(mysql_error());
echo "You successfully deleted the news entry.";
}
}
}
}
?>
しかし、http://www.youngcreativity.com にアクセスしてみてください。
最初のページにエラーが表示されます。
私のサーバーでは、このファイルをインポートしました:
この問題を解決するのを手伝ってください。私は非常に長い間それに固執してきました...