仅在重定向但不使用php键入url时显示页面

时间:2017-02-13 06:24:07

标签: php forms .htaccess

我只是在通过提交按钮重定向时才显示页面。例如 如果主网址为http://www.example.com

 Am having form in `http://wwww.example.com/form.php` and when its submitted its         
 redirected to http://wwww.example.com/success.php

但如果用户在浏览器网址中输入http://wwww.example.com/success.php,如果表单未提交,我该如何将其重定向到form.php

2 个答案:

答案 0 :(得分:0)

请在重定向到success.php&之前在form.php上创建一个SESSION变量。检查success.php上的SESSION变量。就像这样:

在form.php上

<?php
session_start();
$SESSION['user'] = '$user_name';    //Put your username in this or any value which you want to check on success.php page
header("Location : http://wwww.example.com/success.php");
?>

现在成功.php

<?php session_start();
if(!isset($SESSION['user'])){
   header("Location : http://wwww.example.com/form.php");
}

//Now your success.php code & content
?>

希望,这可能对你有所帮助。

答案 1 :(得分:0)

关于success.php

<?php if (realpath(__FILE__) == realpath($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'])) {header('Location: http://wwww.example.com/form.php');die();}?>