【问题标题】:Redirection from PHP file to FORM PHP [duplicate]从 PHP 文件重定向到 FORM PHP [重复]
【发布时间】:2020-03-29 20:20:28
【问题描述】:

我需要有关重定向的帮助。我有一个用 HTML 简单形式编写的表单。它正在获取用户名和密码并将它们发布到另一个 php 文件。这个 PHP 文件正在检查用户和密码是否为假;我想重定向到后台表单文件以再次获取用户名和密码。但在重定向到表单页面之前,我想向用户显示错误和重定向消息。 也许,我们可以在表单页面中显示此消息,也许在重定向之前。 我们该如何处理呢?你能帮我解决这个问题吗?

表格文件:

<html>
<body>

<form action="cvphost.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit">
</form>

</body>
</html>

PHP 文件:

<pre>
<?php
#GETTING EXISTING HOST FILE OF ANSIBLE
$host_file=file('/etc/ansible/hosts');
#GETTING VALUE OF USERNAME,PASS LINE OF HOSTFILE
$ansibleusername=$host_file[7];
$ansiblepassword=$host_file[8];
#PARSING USERNAME AND PASS LINE OF THAT HOST FILE
$list1=split('=',$ansibleusername);
$list2=split('=',$ansiblepassword);
#UPDATING USERNAME AND PASS FROM FORM
$list1[1]=$_POST["username"];
$list2[1]=$_POST["password"];
#JOINING WITH NEW VALUE AND INSERTING HOST-FILE LINES
$array1=array($list1[0],$list1[1]);
$array2=array($list2[0],$list2[1]);
$newansibleusername=join("=",$array1);
$newansiblepassword=join("=",$array2);
$host_file[7]=$newansibleusername."\n";
$host_file[8]=$newansiblepassword."\n";
#PUSHING NEW FILE TO ANSIBLE HOST FILE
$updated_host_file=file_put_contents('/etc/ansible/hosts',$host_file, LOCK_EX);
#usleep(1000);
#chdir('/etc/ansible/');
#$pwd=shell_exec('pwd');
#$task=shell_exec('ansible-playbook user-login-playbook.yml');
#echo $task;
#echo $pwd;
$log_file=file('/etc/ansible/log/user-login-playbook-log.txt');
***#THIS PART IS REDIRECTION PART OF MY CODE. ABOVE IS CORRECT, I WANT FIX BELOW PART***
if ($log_file[0]=="YOU CAN MANAGE CVP NOW")
   { print_r ($log_file[0]);}
else
 {  print_r ($log_file[1])."\n";
    print "<pre>";
    print_r('REDIRECTING TO LOGIN PAGE!...');
}**

【问题讨论】:

标签: php html redirect


【解决方案1】:

redirect 条件中添加标题:

header('Location: /login.php');

【讨论】:

  • 如果我添加标题,用户看不到带有错误消息的 php 页面。它只是刷新表单页面。当用户名/密码错误时,我应该会看到错误消息。无论是在 php 页面还是表单页面中都可以看到错误消息。我现在更喜欢在 php 页面上看到它。然后重定向到返回表单页面。
【解决方案2】:

你可以做的是创建一个文件来处理redirects并打印print_r('REDIRECTING TO LOGIN PAGE!...');,然后你可以使用sleep函数hereredirect它们到所需的页面

您的文件将如下所示:

print_r('REDIRECTING TO LOGIN PAGE!...');
sleep(5); //in seconds
header('Location: /a/location')

【讨论】:

  • 用户在看到重定向到登录页面后如何再次看到表单页面?
  • 另一种方法是在用户端使用javascript,点击浏览器上的返回按钮
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多