重定向主页

时间:2017-01-09 06:04:40

标签: php redirect

  

Iam学习PHP,所以我已经创建了一个网站..我已经创建了index.php和index_1.php以及login.php,其中当用户成功登录时,用户必须重定向到index.php但是我没有得到它所以我在这个页面中创建了另一个index_1.php只有三个导航栏将在那里Home,projects和contactus。   这是我的index.php页面

   <!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

   <?
 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

  <?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并登录,除非他按下注销按钮,否则他不应该看到注册标签

2 个答案:

答案 0 :(得分:1)

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

  • 用户将登录表单提交给login.php,如果登录正确,将设置$_SESSION变量以指示用户已登录。
  • 然后将用户重定向回网站,
  • 在模板中,登录/注销用户可选的任何内容都包含在IF

像这样:

     <?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 :(得分:0)

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

之后使用以下代码编辑您的索引页面,首先我们将检查是否已创建[user_email]会话,如果它已创建,我们不会回显任何内容,如果没有,那么我们将回显登录和注册链接。 bellow是你的新index.php页面

<?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>