我正致力于设计一个基本的社交媒体网站,我喜欢的按钮总是将我重定向到登录页面而不是实际上喜欢'这篇文章。下面是我正在使用的代码。任何帮助都会很棒。
<?php
include('./classes/Login.php');
$isfollowing = False;
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
} else {
$users = $_SESSION['username'];
}
$username = $_GET['u'];
if(DB::query("SELECT username, fname FROM users WHERE username='$username'")) {
$user = $username;
$userid = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
$followerid = $users;
if(isset($_POST['post'])) {
$userid = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
$postbody = $_POST['postbody'];
if (strlen($postbody) > 300 || strlen($postbody) < 3) {
die('Invalid Length.');
}
DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0)', array(':postbody'=>$postbody, ':userid'=>$userid));
}
if(isset($_POST['like'])) {
DB::query('UPDATE posts SET likes = likes+1 where user_id=:user');
DB::query('INSERT INTO post_likes VALUES (\'\', :postid, :userid)', array(':postid'=>$postId, ':userid'=>$followerid));
}
if(isset($_POST['unlike'])) {
DB::query('UPDATE posts SET likes = likes+1 where user_id=:user');
DB::query('DELETE FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$postId, ':userid'=>$followerid));
}
$dbposts = DB::query('SELECT * FROM posts WHERE user_id=:userid ORDER BY id DESC', array(':userid'=>$userid));
$posts = "";
foreach($dbposts as $p) {
if(!DB::query('SELECT post_id FROM post_lilkes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$p['id'], ':userid'=>$users))) {
$posts .= htmlspecialchars($p['body'])."
<form action='profile.php?u=".$username."&postid=".$p['id']." method='post'>
<input type='submit' name='like' value='Like!'>
<span>".$p['likes']."</span>
</form>
<hr /></br />";
} else {
$posts .= htmlspecialchars($p['body'])."
<form action='profile.php?u=".$username."&postid=".$p['id']." method='post'>
<input type='submit' name='unlike' value='Unlike'>
<span>" .$p['likes']."</span>
</form>
<hr /></br />";
}
}
} else {
header('Location: login.php');
}
?>
&#13;
运行var_dump($_SESSION, $_POST); exit;
后,我得到以下输出:array(1) { ["username"]=> string(7) "ankoryx" } array(0) { }