Php 如何显示不同的会话状态';在配置文件页面上(来宾视图与用户配置文件视图)
我正在创建一个好友系统,当用户访问另一个用户的个人资料时,他们会看到一个添加好友选项,当他们访问自己的个人资料时,他们会看到另一个选项,而不是添加好友选项,如好友总数(或类似选项),当客人访问(未登录)时,他们对这一页的看法也不同。总而言之,我需要在一个页面上显示三个视图:Php 如何显示不同的会话状态';在配置文件页面上(来宾视图与用户配置文件视图),php,mysql,session,Php,Mysql,Session,我正在创建一个好友系统,当用户访问另一个用户的个人资料时,他们会看到一个添加好友选项,当他们访问自己的个人资料时,他们会看到另一个选项,而不是添加好友选项,如好友总数(或类似选项),当客人访问(未登录)时,他们对这一页的看法也不同。总而言之,我需要在一个页面上显示三个视图: 当用户未登录时 登录时user!=用户id 登录时user==user\u id 就我现在所知,我的脚本设置出现了一些问题。在适当的情况下,会话似乎不起作用 header.php: <?php include ( "
user!=用户id
user==user\u id
<?php
include ( "./inc/connect.inc.php" );
session_start();
if (isset($_SESSION['user_login'])) {
$user = $_SESSION['user_login'];
}
else {
$user = "";
}
?>
<?php include ( "./inc/header.inc.php" );
if (isset($_GET['u'])) {
$username = mysql_real_escape_string($_GET['u']);
if (ctype_alnum($username)) {
//check user exists
$check = mysql_query("SELECT username, first_name FROM users WHERE username='$username'");
if (mysql_num_rows($check)===1) {
$get = mysql_fetch_assoc($check);
$username = $get['username'];
$firstname = $get['first_name'];
}
else {
echo "<meta http-equiv=\"refresh\" content=\"0; url=http://localhost/tutorials/index.php\">";
exit();
}
}
}
$optionbox = "";
if (isset($_SESSION['user_login']) != $user){
$optionbox = '<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">
<a href="#">Add friend</a>
</div>';
}
else if (isset($_SESSION['user_login']) == $user){
$optionbox = '<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">
<a href="#">friend list</a>
</div>';
}
else {
$optionbox = '<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">
</div';
}
?>
profile.php:
<?php
include ( "./inc/connect.inc.php" );
session_start();
if (isset($_SESSION['user_login'])) {
$user = $_SESSION['user_login'];
}
else {
$user = "";
}
?>
<?php include ( "./inc/header.inc.php" );
if (isset($_GET['u'])) {
$username = mysql_real_escape_string($_GET['u']);
if (ctype_alnum($username)) {
//check user exists
$check = mysql_query("SELECT username, first_name FROM users WHERE username='$username'");
if (mysql_num_rows($check)===1) {
$get = mysql_fetch_assoc($check);
$username = $get['username'];
$firstname = $get['first_name'];
}
else {
echo "<meta http-equiv=\"refresh\" content=\"0; url=http://localhost/tutorials/index.php\">";
exit();
}
}
}
$optionbox = "";
if (isset($_SESSION['user_login']) != $user){
$optionbox = '<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">
<a href="#">Add friend</a>
</div>';
}
else if (isset($_SESSION['user_login']) == $user){
$optionbox = '<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">
<a href="#">friend list</a>
</div>';
}
else {
$optionbox = '<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">
</div';
}
?>
有几件事可以帮助你。为清楚起见,我已记下:
<?php
// Add connection here...(should be a PDO or mysqli_)
session_start();
// Add a simple true/false function to test for logged in
// Would be better included on this page rather than written (like connection)
function is_loggedin()
{
return (!empty($_SESSION['user_login']));
}
// Make a function to test if the logged-in user is currently viewing own profile
// Would be better included on this page rather than written (like connection)
function is_profile($user = false)
{
if(!$user)
return false;
return (is_loggedin() && ($_SESSION['user_login'] == $user));
}
// Presumably you would have profile.php?u=23432 or similar
// If no user requested just assign false
$user = (!empty($_GET['u']) && ctype_alnum($_GET['u']))? $_GET['u'] : false;
// If the user is valid (not empty)
// Would be better included on this page if condition is met otherwise
// It just kind of loads extra lines for nothing
if($user != false) {
// You should not be using mysql_ anymore, PDO or mysqli_ with prepared/bound statements
$username = mysql_real_escape_string($user);
//check user exists
$check = mysql_query("SELECT username, first_name FROM users WHERE username='$username'");
if (mysql_num_rows($check) === 1) {
$get = mysql_fetch_assoc($check);
$username = $get['username'];
$firstname = $get['first_name'];
}
else {
echo "<meta http-equiv=\"refresh\" content=\"0; url=http://localhost/tutorials/index.php\">";
exit;
}
}
// Just do one wrapper
$optionbox[] = '<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">';
// Check if a user is logged in
if(is_loggedin())
// If the logged in user matches the $_GET user
$optionbox[] = (!is_profile($user))? '<a href="#">Add friend</a>' : '<a href="#">friend list</a>';
// If user is not logged in
else
$optionbox[] = '<h3>You must be logged in to view stuff and things</h3>';
// Finish wrapper
$optionbox[] = '</div>';
// Write to page
echo implode(PHP_EOL,$optionbox);
?>
/classes/class.Database.php
这里的错误说明了什么?请具体说明。你从哪里得到的$\u get['u']
?你刚才说你有header.php
为什么你要这样包含它include(“./inc/header.inc.php”)代码>?您应该这样写吗include(“./inc/header.php”)代码>?您在此处哪里定义了$user
?我认为你的代码中没有这方面的价值。你在所有情况下都使用过的那一个。在你有(isset($\u SESSION['user\u login'])=$user)
之类的地方,试试(isset($\u SESSION['user\u login'])&&$\u SESSION['user\u login']=$user)
对不起,这一行我搞错了。应该是:$user=(!empty($\u GET['u'])和&ctype_alnum($\u GET['u'])$_GET['u']:false代码>当然可以,您是否完全像我上面所说的那样拥有它?您熟悉使用对象(类)吗?我认为在这里使用类更合适。同样的方法,但是它包含的更好。如果您能在这方面帮助我,我将非常感谢您,只是为follow system添加了新的代码您在吗?也许发电子邮件比在这里更好。
<?php
// This class can be expanded out to do a lot more than just fetch from the database
class QueryEngine
{
protected $query;
private static $singleton;
// Create and Return instance of itself
public function __construct()
{
if(!empty(self::$singleton))
self::$singleton = $this;
return self::$singleton;
}
// Basic query method
public function query($sql = false, $bind = false)
{
AutoloadFunction("PDOConnect");
$con = PDOConnect();
$this->query = $con->prepare($sql);
if(!empty($bind)) {
foreach($bind as $key => $val) {
$bindVals[":{$key}"] = $val;
}
$this->query->execute($bindVals);
}
else
$this->query->execute();
return $this;
}
// This function is what returns the array in conjunction with
// the query method
public function Fetch()
{
if($this->query) {
while($row = $this->query->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
}
return (!empty($result))? $result : 0;
}
}
<?php
// This function is what is used to autoload classes on the fly
// There is no need to include class files, so long as they are in the
// /classes/ folder
function AutoLoadClasses($className) {
if(class_exists($className)) {
return;
}
if(is_file(CLASSES_DIR."/class.".$className.'.php')) {
include_once(CLASSES_DIR."/class.".$className.'.php');
}
}
<?php
// This will attempt to load the required file to run a specified function
// Similar to the autoloader for the classes, only this is required to be
// manually called like: AutoloadFunction("myfunction"); before function use
function AutoloadFunction($function = false,$loaddir = false)
{
if($function == false)
return false;
if(strpos($function,","))
$functions = explode(",",$function);
$function_dir = ($loaddir != false && !is_array($loaddir))? $loaddir.'/function.': FUNCTIONS_DIR.'/function.';
if(!isset($functions)) {
$functions[] = $function;
}
for($i = 0; $i < count($functions); $i++) {
// Function name
$addfunction = $functions[$i];
// See if function exists
if(!function_exists($addfunction)) {
$dir = $function_dir.$addfunction.'.php';
if(is_file($dir)) {
include_once($dir);
}
}
}
}
<?php
// Just make a quick PDO function to return your PDO Connection
// populate the arguements with your database credentials
function PDOConnect($user = "username",$pass = "password",$host = "hostname",$data = "databasename")
{
return Database::connect($user,$pass,$host,$data);
}
<?php
// Here is a quick array fetching function using the query engine class
function query_fetch($sql = false,$bind = false)
{
$qEngine = new QueryEngine();
return $qEngine ->query($sql,$bind)
->Fetch();
}
<?php
// Add a simple true/false function to test for logged in
function is_loggedin()
{
return (!empty($_SESSION['username']));
}
<?php
// Make a function to test if the logged-in user is currently viewing own profile
function is_profile($user = false)
{
if(!$user)
return false;
AutoloadFunction("is_loggedin");
return (is_loggedin() && ($_SESSION['username'] == $user));
}
<?php
// This will fetch the user based on a get variable
function get_profile($username = false)
{
// Presumably you would have profile.php?u=23432 or similar
// If no user requested just assign false
$user = (!empty($_GET['u']) && ctype_alnum($_GET['u']))? $_GET['u'] : false;
$array['username'] = false;
$array['first_name'] = false;
// If the user is valid (not empty)
// Would be better included on this page if condition is met otherwise
// It just kind of loads extra lines for nothing
if($user != false) {
AutoloadFunction("query_fetch");
//check user exists
$get = query_fetch("SELECT `username`, `first_name`,`ID` FROM `users` WHERE `username` = :0",array($user));
if ($get != 0) {
$array['username'] = $get[0]['username'];
$array['ID'] = $get[0]['ID'];
$array['first_name'] = $get[0]['first_name'];
return ($username)? $array['username'] : $array;
}
else {
header("location: http://localhost/tutorials/index.php");
exit;
}
}
return $array;
}
<?php
// Define some basic locational constants
define("ROOT_DIR",__DIR__);
define("CLASSES_DIR",ROOT_DIR."/classes");
define("FUNCTIONS_DIR",ROOT_DIR."/functions");
// Load up the functions autoloader
include_once(FUNCTIONS_DIR."/function.AutoloadFunction.php");
// Load up the function for class autoloading
AutoloadFunction("AutoLoadClasses");
// Apply the autoloader for classes
spl_autoload_register('AutoLoadClasses');
<?php
session_start();
// Load all the settings to make things work.
include(__DIR__."/config.php");
?>
<div style="border:#CCC 1px solid; padding:5px; background-color:#E4E4E4; color:#999; font-size:11px;">
<?php
// Using the "AutoloadFunction" should save execution time because
// it will only load functions it needs instead of loading all the functions
// it could "potentially" need.
AutoloadFunction("is_loggedin");
// Check if a user is logged in
if(is_loggedin()) {
AutoloadFunction("get_profile,is_profile");
// Fetch the profile of current user query
$user = get_profile();
// If the logged in user matches the $_GET user
echo (!is_profile($user['username']))? '<a href="?action=add&u='.$user['ID'].'">Add '.ucwords($user['first_name']).'</a>' : '<a href="#">friend list</a>';
}
// If user is not logged in
else {
?>
<h3>You must be logged in to view stuff and things</h3>
<?php }
?>
</div>