我有一个 Php 索引页面和一个登录链接。当用户登录到该网站时,我想重定向到同一页面(索引.php)。在登录链接的位置,我想显示用户或客户端的用户名。我有一个这样的 php 登录代码
$error = '';
if (isset($_POST['login']) && !empty($_POST['email'])
&& !empty($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$sel_user = "select * from customer where email='$email' AND password='$password'";
$run_user = mysql_query($sel_user);
$check_user = mysql_num_rows($run_user);
if($check_user == 1){
$_SESSION['email']=$email;
echo "<script>window.open('userinfo.php','_self')</script>";
}
else {
$error = 'Invalid username or password';
}
}
在此代码页中,登录后重定向到另一个页面,但我想将其重定向到与index.php相同的页面。并添加链接,例如我的帐户和注销链接,而不是索引中的登录链接.php。我有一个这样的 Php 会话页面代码
if(!isset($_SESSION))
{
session_start();
}
$login_session=$_SESSION['email'];
if(!isset($_SESSION['email']))
{
// not logged in
header('Location: login.php');
exit();
}
例如,我怎样才能像 flipkart.com 一样做到这一点。请帮助我。
你可以做一件事:
选择您已经为登录编写的代码,即
$error = '';
if (isset($_POST['login']) && !empty($_POST['email'])
&& !empty($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$sel_user = "select * from customer where email='$email' AND password='$password'";
$run_user = mysql_query($sel_user);
$check_user = mysql_num_rows($run_user);
if($check_user == 1){
$_SESSION['email']=$email;
echo "<script>window.open('userinfo.php','_self')</script>";
}
else {
$error = 'Invalid username or password';
}
}
在此之后,您检查会话变量
if(!isset($_SESSION))
{
session_start();
}
$login_session=$_SESSION['email'];
if(!isset($_SESSION['email']))
{
// not logged in
header('Location: index.php'); //change the redirect page to index.php
//now customize the menu accoringly
echo "<nav class='your-class'>";
echo "<li>";
echo "<ul>Login</ul>";
//Some more menus
echo "</nav>";
exit();
}
else
{
header('Location: index.php'); //now configure the menu accordingly
//now customize the menu accoringly on login
echo "<nav class='your-class'>";
echo "<li>";
echo "<ul><a href="some link">My Account</a></ul>";
//Some more menus
echo "<ul><a href="logout.php">Logout</a></ul>";
echo "</nav>";
}
但据我所知,这有点烦人,请尽量避免这样的查询。请改用 PDO。谢谢。
试试这个
header('location:'.$_SERVER['PHP_SELF']);
而不是
echo "<script>window.open('userinfo.php','_self')</script>";
以及用于管理链接
<ul>
<?php if(isset($_SESSION['email'])){ ?>
<li><a href="logout.php" >Logout</a></li>
<?php }else{ ?>
<li><a href="index.php" >Home</a></li>
<?php }?>
</ul>