简体   繁体   English

修改PHP函数以在操作后重定向

[英]Modifying PHP function to redirect after action

I'd like the following PHP code to redirect to a specified page after completion of the function. 我想在功能完成后,将以下PHP代码重定向到指定的页面。 I tried a few different methods but it doesn't ever seem to do anything. 我尝试了几种不同的方法,但似乎什么也没做。 The current action that this is taking is that after the button to call the function is pressed, it redirects to an empty page. 当前正在执行的操作是在按下调用函数的按钮之后,它将重定向到空白页。 Any help would be much appreciated. 任何帮助将非常感激。

    function quote_upvote_button($quoteid, $canvote, $ajaxy=FALSE)
    {
        $s = ' class="quote_plus" id="quote_plus_'.$quoteid.'"';
        if (!$canvote) {
            $url = '<a href="?'.urlargs('vote',$quoteid,'plus').'" '.$s.' title="'.lang('upvote').'">+</a>';
                if ($ajaxy) {
                    return '<script type="text/javascript">document.write(\'<a href="javascript:ajax_vote('.$quoteid.',1);" '.$s.' title="'.lang('upvote').'">+</a>\');
                    </script><noscript>'.$url.'</noscript>';
        }
        return $url;
    }
    return '<span '.$s.' title="'.lang('cannot_vote_'.$canvote).'">+</span>';
}

This will work, $your_redirect_url is the location it will go to. 这将起作用, $your_redirect_url是它将去的位置。 It can either be in relative (/foo/bar) or fully qualified ( http://foo.com/bar ) formats. 它可以是相对(/ foo / bar)或完全限定( http://foo.com/bar )格式。 You can read more on PHP Header Redirects here. 您可以在此处阅读有关PHP标头重定向的更多信息。 Please be aware that Miinimals answer requires an additional PECL library to execute, where this is an ingrain function. 请注意,Miinimals答案需要一个额外的PECL库来执行,这是一个根深蒂固的功能。

function quote_upvote_button($quoteid, $canvote, $ajaxy = false, $your_redirect_url = '/')
{
    $s = ' class="quote_plus" id="quote_plus_'.$quoteid.'"';
    if (!$canvote) {
        $url = '<a href="?'.urlargs('vote',$quoteid,'plus').'" '.$s.' title="'.lang('upvote').'">+</a>';
        if ($ajaxy) 
        {
            // Redirect to your location, and terminate...
            // Will redirect to root on default, or can be overridden/modified as an arg
            header("Location: $your_redirect_url");
            exit;
        }
        return $url;
    }
    return '<span '.$s.' title="'.lang('cannot_vote_'.$canvote).'">+</span>';
}

You should use the http_redirect documented here: 您应该使用此处记录的http_redirect:

http://php.net/manual/en/function.http-redirect.php http://php.net/manual/zh/function.http-redirect.php

The redirect should go before the return statement. 重定向应该在return语句之前进行。

You first find the url then redirect to that url by 您首先找到该网址,然后通过以下方式重定向到该网址

   header('location:'.$url);

or in js u can use 或在js中,您可以使用

   window.location.assign($url)
   window.location="http://www.newlocation.com";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM