English 中文(简体)
如何改用PHP的前页?
原标题:How to redirect back to the previous page in PHP?
  • 时间:2011-11-22 08:08:01
  •  标签:
  • php

I have a PHP site which have a login page. I have also an order.php page. When the user is not login then the order page redirect the user to the login page first. I want when the user redirect from the order page to the login page then on successfully login the user should redirect back to the order page. And When the user comes from other pages to the login page then on successfully login the user should redirect to the index.php page. How will I do this? Any Idea?

最佳回答

当用户没有登录时,订单页将用户转至日志首页。

Why extra redirect?
Why not to show the login form right in place and upon successful login just redirect to the same page?

这里是短暂的故事。 php page

<?
if (isset($_POST[ auth_name ])) {
  $name  = mysql_real_escape_string($_POST[ auth_name ]);
  $pass  = MD5($_POST[ auth_name ].$_POST[ auth_pass ]);
  $query = "SELECT * FROM users WHERE name= $name  AND pass= $pass ";
  $res   = mysql_query($query) or trigger_error(mysql_error().$query);
  if ($row = mysql_fetch_assoc($res)) {
    session_start();
    $_SESSION[ user_id ] = $row[ id ];
  }
  header("Location: http://".$_SERVER[ HTTP_HOST ].$_SERVER[ REQUEST_URI ]);
  exit;
}
if (isset($_GET[ action ]) AND $_GET[ action ]=="logout") {
  session_start();
  session_destroy();
  header("Location: http://".$_SERVER[ HTTP_HOST ]."/");
  exit;
}
if (isset($_REQUEST[session_name()])) session_start();
if (empty($_SESSION[ user_id ])) {
  return;
} else {
  include  top.php ;
?>
<form method="POST">
<input type="text" name="auth_name"><br>
<input type="password" name="auth_pass"><br>
<input type="submit"><br>
</form>
<? 
  include  bottom.php ;
}
exit;
?>

现在,你只能使用一行保护任何网页。

require "auth.php";
问题回答

Order.php

<?php 
if (!logged_in()){
   header("Location: 日志?referrer=order");
}
?>

日志

<?php 
if (login_successful()){
   switch($_GET[ referrer ])){
      case  order :
          header("Location: Order.php");
      break;
      case  other :
          header("Location: other.php");
      break;
      default:
          header("Location: index.php");
   }
}
?>

form

<form method="post" action="日志">
    //
    // Form content
    // 
</form>

---replace to---

form phpself

<form method="post" action="<?php echo $_SERVER[ PHP_SELF ]; ?>">
    //
    // Form content
    // 
</form>

Simply use a URL in your link. The url from order.php would be:
http://somesite.com/login.php?url=order.php
In your login page you can then read the $_GET["url"] and decide whether to redirect the user back, or redirect him to index.php.

您可以确定一个跟踪返回页的 co或会议价值。 成功登录后,检查是否有返回网页。 如果是的话,可上页。 否则,就进入默认的欢迎网页。

将标识目标储存到本届会议,然后改用标识页。

例:

  1. The order.php page checks if the user is already logged in and calls login.php if necessary. Before it does the redirect to the login.php page it stores the login target as itself $_SESSION[ loginTarget ] = order.php ;
  2. After the login page has finished successfully, it calls the login target. header( Location: .$_SESSION[ loginTarget ], true, 303); exit;

由于标识目标储存在本届会议上,你可以轻松地在标识页上进行错误处理,甚至可以建立一个新的用户账户,而不是记录(无需将尿中的指标转至每一页)。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签