【问题标题】:Two PHP Forms that Redirect to x/y URL on the same page在同一页面上重定向到 x/y URL 的两个 PHP 表单
【发布时间】:2012-05-11 20:13:50
【问题描述】:

我正在尝试为使用 Wordpress 或 Indexhibit 管理网站的客户创建一个“简单”重定向。

我已将其设置为允许他们输入他们的域并将其重定向到相应的管理区域(我很惊讶有多少人忘记了,但嘿嘿,这是我的解决方案

我让它对于单个重定向工作正常,但是在之前的 SO 提交的后面尝试使用 if / elseif 集成两者并且它无法解析。

任何想法我做错了什么/非常感谢!

<?php 
if ($_POST['indexhibit_url']) {
if($_SERVER['REQUEST_METHOD'] == 'POST') : $indexhibit_url = $_POST['indexhibit_url']; header('Location: http://' . $indexhibit_url . '/ndxz-studio/');
}
elseif ($_POST['wordpress_url']) {
if($_SERVER['REQUEST_METHOD'] == 'POST') : $wordpress_url = $_POST['wordpress_url']; header('Location: http://' . $wordpress_url . '/wp-admin/');
}
?>


<form action="<?php echo $_SERVER['../PHP_SELF']; ?>" method="post" name="indexhibit_url">

<h3>Indexhibit</h3>

<p class='formp'>If your website is powered by <em>Indexhibit</em> submit your URL to be forwarded to your admin area</p>
<input class='loginforms' type="text"  value='i.e. your-domain-name.com' onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'i.e. your-domain-name.com':this.value;"   name="indexhibit_url" />
<input class="btn btn-info loginbuttons" name="index_submit" type="submit" value="Go" />

</form>

<form action="<?php echo $_SERVER['../PHP_SELF']; ?>" method="post" name="wordpress_url">

<h3 style='margin-top:2em;'>Wordpress</h3>

<p class='formp'>If your website is powered by <em>Wordpress</em> submit your URL to be forwarded to your admin area</p>
<input class='loginforms' type="text"  value='i.e. your-domain-name.com' onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'i.e. your-domain-name.com':this.value;"   name="wordpress_url" />
<input class="btn btn-info loginbuttons" name="wp_submit" type="submit" value="Go" />

</form>

<?php endif; ?> 

【问题讨论】:

    标签: php forms url redirect if-statement


    【解决方案1】:

    你混淆了三元和非三元运算符:

    只需使用普通的 if/else 语法:

    if ($_POST['indexhibit_url']) {
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $indexhibit_url = $_POST['indexhibit_url']; 
            header('Location: http://' . $indexhibit_url . '/ndxz-studio/');
        }
    }elseif ($_POST['wordpress_url']) {
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $wordpress_url = $_POST['wordpress_url']; 
            header('Location: http://' . $wordpress_url . '/wp-admin/');
        }
    }
    

    请记住,像这样使用 PHP_SELF 会使您的代码容易受到 xss 注入的攻击

    【讨论】:

    • 完美!是的,我是 PHP 新手,所以仍然会犯很多错误。但这(加上从代码末尾删除 使其完美运行。非常感谢
    • 开发时应使用netbeans、eclipse等IDE,避免语法错误。
    猜你喜欢
    • 1970-01-01
    • 2021-01-04
    • 2019-10-06
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2012-12-08
    相关资源
    最近更新 更多