简体   繁体   English

用户登录后(通过我创建的登录表单)如何将他们发送到其他页面?

[英]Once a user logs in (via a login form I've created) how do I send them to a different page?

I know this is probably a simple question to someone who's been doing this for a while, but please cut me some slack as I am very new to php. 我知道这对于已经进行了一段时间的人来说可能是一个简单的问题,但是请减少一些懈怠,因为我对php非常陌生。

So essentially I want to require visitors to register for my site before being able to view the rest of the content. 因此,从本质上讲,我希望要求访问者注册我的网站之后才能查看其余内容。 I have found an open source login application and attached it to a mysql database and got everything working fine. 我找到了一个开源的登录应用程序,并将其附加到mysql数据库,一切正常。 The problem is, once someone logs in, it sends them back to the same page, just with a greeting (and now they are loggin in.). 问题是,一旦有人登录,它就会带着问候将他们发送回同一页面(现在他们已登录)。 How can I redirect them to a different page once they log in? 他们登录后如何将他们重定向到其他页面?

This is the open source script I am using: 这是我正在使用的开源脚本:

<?

/**
 * Checks whether or not the given username is in the
 * database, if so it checks if the given password is
 * the same password in the database for that user.
 * If the user doesn't exist or if the passwords don't
 * match up, it returns an error code (1 or 2). 
 * On success it returns 0.
 */
function confirmUser($username, $password){
   global $conn;
   /* Add slashes if necessary (for query) */
   if(!get_magic_quotes_gpc()) {
    $username = addslashes($username);
   }

   /* Verify that user is in database */
   $q = "select password from users where username = '$username'";
   $result = mysql_query($q,$conn);
   if(!$result || (mysql_numrows($result) < 1)){
      return 1; //Indicates username failure
   }

   /* Retrieve password from result, strip slashes */
   $dbarray = mysql_fetch_array($result);
   $dbarray['password']  = stripslashes($dbarray['password']);
   $password = stripslashes($password);

   /* Validate that password is correct */
   if($password == $dbarray['password']){
      return 0; //Success! Username and password confirmed
   }
   else{
      return 2; //Indicates password failure
   }
}

/**
 * checkLogin - Checks if the user has already previously
 * logged in, and a session with the user has already been
 * established. Also checks to see if user has been remembered.
 * If so, the database is queried to make sure of the user's 
 * authenticity. Returns true if the user has logged in.
 */
function checkLogin(){
   /* Check if user has been remembered */
   if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
      $_SESSION['username'] = $_COOKIE['cookname'];
      $_SESSION['password'] = $_COOKIE['cookpass'];
   }

   /* Username and password have been set */
   if(isset($_SESSION['username']) && isset($_SESSION['password'])){
      /* Confirm that username and password are valid */
      if(confirmUser($_SESSION['username'], $_SESSION['password']) != 0){
         /* Variables are incorrect, user not logged in */
         unset($_SESSION['username']);
         unset($_SESSION['password']);
         return false;
      }
      return true;
   }
   /* User not logged in */
   else{
      return false;
   }
}

/**
 * Determines whether or not to display the login
 * form or to show the user that he is logged in
 * based on if the session variables are set.
 */
function displayLogin(){
   global $logged_in;
   if($logged_in){
      echo "<h1>Logged In!</h1>";
      echo "Welcome <b>$_SESSION[username]</b>, you are logged in. <a href=\"logout.php\">Logout</a>";
   }
   else{
?>

<form action="" method="post">
<table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td  align="left"><input type="checkbox" name="remember">
<font size="2">Remember me </td>
<td align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
</table>
</form>

<?
   }
}


/**
 * Checks to see if the user has submitted his
 * username and password through the login form,
 * if so, checks authenticity in database and
 * creates session.
 */
if(isset($_POST['sublogin'])){
   /* Check that all fields were typed in */
   if(!$_POST['user'] || !$_POST['pass']){
      die('You didn\'t fill in a required field.');
   }
   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("Sorry, the username is longer than 30 characters, please shorten it.");
   }

   /* Checks that username is in database and password is correct */
   $md5pass = md5($_POST['pass']);
   $result = confirmUser($_POST['user'], $md5pass);

   /* Check error codes */
   if($result == 1){
      die('That username doesn\'t exist in our database.');
   }
   else if($result == 2){
      die('Incorrect password, please try again.');
   }

   /* Username and password correct, register session variables */
   $_POST['user'] = stripslashes($_POST['user']);
   $_SESSION['username'] = $_POST['user'];
   $_SESSION['password'] = $md5pass;

   /**
    * This is the cool part: the user has requested that we remember that
    * he's logged in, so we set two cookies. One to hold his username,
    * and one to hold his md5 encrypted password. We set them both to
    * expire in 100 days. Now, next time he comes to our site, we will
    * log him in automatically.
    */
   if(isset($_POST['remember'])){
      setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
      setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
   }

   /* Quick self-redirect to avoid resending data on refresh */
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}

/* Sets the value of the logged_in variable, which can be used in your code */
$logged_in = checkLogin();

?>

Now I am smart enough to know (or at least think) that I need to add something to 现在我很聪明,知道(或至少认为)我需要添加一些内容

function displayLogin(){
   global $logged_in;
   if($logged_in){
      echo "<h1>Logged In!</h1>";
      echo "Welcome <b>$_SESSION[username]</b>, you are logged in. <a href=\"logout.php\">Logout</a>";
   }
   else{

I just don't know what to add to that piece of code to redirect them to a different page. 我只是不知道要在这段代码中添加些什么来将它们重定向到另一个页面。 Can anyone please help? 谁能帮忙吗?

<?php header("Location: http://url"); ?>

可能还有其他需要考虑的因素。

<?php
   header( 'Location: any-page.php' ) ;
?>

Make sure that every page that needs to be protected calls the checkLogin() function before any other code. 确保需要保护的每个页面在调用任何其他代码之前都调用checkLogin()函数。 In that function, also look below /* User not logged in */ You should probably also put a redirect in there back to your login page. 在该功能中,还请查看/ *用户未登录* /下方的内容,您可能还应该在其中将重定向重定向回您的登录页面。

After successfull log in 成功登录后

header("Loaction: http://www.your.new.site");

http://us3.php.net/manual/en/function.header.php http://us3.php.net/manual/zh/function.header.php

or 要么

http_redirect ("http://www.your.new.site");

http://us3.php.net/manual/en/function.http-redirect.php http://us3.php.net/manual/zh/function.http-redirect.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户未通过身份验证时,如何让Laravel发送登录表单,而不是重定向到登录页面? - How do I have Laravel send a login form when a user isn't authenticated, rather than redirecting to a login page? 我正在使用弹出窗口进行用户登录,当用户实际登录时如何刷新父(打开)页面? - I'm using a popup for user login, how do i refresh the parent(openner) page when user actually logs in? 用户登录后如何将登录状态更改为注销 - How do I change login status to logout after user logs in 如何一次处理通过HTML表单上传到PHP页面的多个文件 - How do I handle multiple files at once uploaded to a PHP page via an HTML form 如何为创建的每个多边形打开不同的信息? Google Maps API v3 - How do i open different information for each polygon i've created? Google maps api v3 如何遍历页面上的表单域并将其发送到数据库? - How do I iterate through form-fields on a page and send them to a database? 如果在出现错误时将用户重新填充回表单后,如何重新填写用户填写的表单字段? - How can I re-fill form fields that a user filled out after I've redirected them back to the form when there was an error? 登录后如何将用户引导到不同的页面? - How can I direct users to different pages once they've logged in? 如何获取所有表单输入并将其发送到电子邮件? - How do I get all form inputs and send them to an email? 如何使用我创建的原始 php 函数? - how do i use original php functions i've created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM