首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重定向HomePage

重定向HomePage
EN

Stack Overflow用户
提问于 2017-01-09 06:04:40
回答 2查看 66关注 0票数 0

我学习PHP,所以我启动了一个website..in,我已经创建了一个index.php和index_1.php以及login.php,当用户成功登录时,用户必须被重定向到index.php,但是我没有得到它,所以我在这个页面中创建了另一个index_1.php,只有三个导航栏将在主页、项目和contactus。这是我的index.php页面

代码语言:javascript
复制
   <!DOCTYPE HTML>
   <html>
    <head>
  <title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
  <div id="logo">
    <div id="logo_text">
      <!-- class="logo_colour", allows you to change the colour of the text -->
      <h1><a href="index.php">KarthikAenugula</a></h1>

    </div>
  </div>
  <div id="menubar">
    <ul id="menu">
      <!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
      <li class="selected"><a href="index.php">Home</a></li>
      <li><a href="project.php">Projects</a></li>
      <li><a href="aboutme.php">AboutMe</a></li>
      <li><a href="login.php">Login</a></li>
      <li><a href="register.php">Register</a></li>
      <li><a href="contact.php">Contact</a></li>

      </ul>
  </div>
</div>

这是我的index_1.php

代码语言:javascript
复制
   <?
 session_start();
  if(!isset($_SESSION['user_email']))
    {
   echo '<p>Please Login to continue <a href="login.php">Log In</a></p>';
   exit();
    }
   ?>


   <!DOCTYPE HTML>
   <html>

  <head>
  <title>Karthik</title>
  <meta name="description" content="website description" />
  <meta name="keywords" content="website keywords, website keywords" />
  <meta http-equiv="content-type" content="text/html; charset=windows-1252" />
  <link href="style/style.css" rel="stylesheet" type="text/css">
  <style>
 .mySlides {display:none;}
  </style>
  </head>
  <body>
  <div id="main">
  <div id="header">
  <div id="logo">
    <div id="logo_text">
      <!-- class="logo_colour", allows you to change the colour of the text -->
      <h1><a href="index.php">KarthikAenugula</a></h1>

    </div>
  </div>
  <div id="menubar">
    <ul id="menu">
      <!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
      <li class="selected"><a href="index_1.php">Home</a></li>
      <li><a href="project1.php">Projects</a></li>
      <li><a href="aboutme1.php">AboutMe</a></li>
      <li><a href="contact1.php">Contact</a></li>

                <?php 
                echo '<p align="right">'; 
                session_start();
                echo "Welcome";
                echo '<br>';
                  echo  ($_SESSION ['user_email']); 
                    echo '<br> Logout?<a href="logout.php">ClickHere</a></p>';
                  ?> 

这是我的login.php

代码语言:javascript
复制
  <?php

 ob_start();

session_start();

if(isset($_POST["Submit"]))
   {
  $user_email=$_POST['user_email'];
  $user_password=md5($_POST['user_password']);
  $con=@mysql_connect('localhost','xxxxx','xxxx') or                     die(mysql_error());
  mysql_select_db('suryapra_aenugula_karthik');
  $query=mysql_query("SELECT * FROM user_registration where user_email='".$user_email."' AND user_password='".$user_password."'") or die("error in selection");
 $numrows=mysql_num_rows($query);
if($numrows!=0)
    {
while($row=mysql_fetch_assoc($query))
     {
    $dbusername=$row['user_email'];
    $dbpassword=$row['user_password'];
    }
 if($user_email==$dbusername && $user_password==$dbpassword)
     {
 if(isset($_POST['remember']))
   {
setcookie('user_email',$user_email,time()+60*60*7);
setcookie('user_password',$user_email,time()+60*60*7);
}
session_start();
$_SESSION['user_email']=$user_email;
header("Location: index_1.php");
ob_end_flush();
    }
  }
    else
   {    
header("Location: login_2.php");
            ob_end_flush();

 }
 }
 else
{
   header("Location: login.php");
}
?>

我的问题是,如果他重定向到index.php的用户登录,再一次登录和注册链接也进入了index.php --我想要解决方案:当用户登录应该重定向到index.php和登录时,除非他按注销按钮,否则注册选项卡对他是不可见的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-09 06:16:08

用户身份验证的基本流程:

  • 用户向login.php提交登录表单,如果登录正确,将设置一个$_SESSION变量以指示用户登录。
  • 然后,用户被重定向回站点,其中
  • 在模板中,对loggedin/注销用户可选的任何内容都封装在IF块中。

差不多是这样的:

代码语言:javascript
复制
     <?php
     $logged_in = $_SESSION['logged_in'];
     ?>

     <nav>
         <?php if (!$logged_in):?>
             <a href="login-form.php">Login</a>
         <?php endif;?>
         <?php if ($logged_in):?>
             <a href="logout.php">Logout</a>
         <?php endif;?>
     </nav>
票数 1
EN

Stack Overflow用户

发布于 2017-01-09 06:37:59

首先,在登录页面成功登录后,在index.php页面上重定向用户。你不需要index_2.php页面。

在此之后,用下面的代码编辑索引页,首先我们将检查是否创建了user_email会话,或者在登录时没有,如果没有,我们没有回显任何内容,如果没有,我们将回显登录和注册链接。贝娄是你新的index.php页面

代码语言:javascript
复制
<?php session_start();

 ?> 
 <!DOCTYPE HTML>
   <html>
    <head>
  <title>Karthik</title>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-1252" />
<link href="style/style.css" rel="stylesheet" type="text/css">
<style>
.mySlides {display:none;}
</style>
</head>
<body>
<div id="main">
<div id="header">
  <div id="logo">
    <div id="logo_text">
      <!-- class="logo_colour", allows you to change the colour of the text -->
      <h1><a href="index.php">KarthikAenugula</a></h1>

    </div>
  </div>
  <div id="menubar">
    <ul id="menu">
      <!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
      <li class="selected"><a href="index.php">Home</a></li>
      <li><a href="project.php">Projects</a></li>
      <li><a href="aboutme.php">AboutMe</a></li>
      <?php if(isset($_SESSION[user_email]))
      {

      }
      else
      {
       echo "<li><a href='login.php'>Login</a></li>";
       echo "<li><a href='register.php'>Register</a></li>";
      }

      ?>
      <li><a href="contact.php">Contact</a></li>

      </ul>
  </div>
</div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41541888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档