这个片段有什么问题

Mat*_*iby -3 javascript php

<?php } elseif($_SOMETHING == 1 && $_ANOTHER_THING == 2) { ?>
<?php $_NAME = urlencode($_NAME); ?>
<?php $_MGT_NAME = urlencode($_MGT_NAME); ?>
</div>    
<?php } ?>
Run Code Online (Sandbox Code Playgroud)

我预计会出现此错误';'

bob*_*nce 5

惊恐的事件.惊恐的事件.

这是onclick属性值中的实际错误:

lpButtonCTTUrl = 'http:...Ad%20Source=somesite.com& ='+escape(document.location); imageUrl=<?php print "http://{$_SERVER['SITENAME']}/images/";?>&referrer
Run Code Online (Sandbox Code Playgroud)

也就是说,应该有一个+'而不是;document.location包含之后,并且在imageURL包含之后应该有一个结束引用,并且referrer位于错误的位置(它应该在document.location包含之前.

它也有使用escape(从不使用escape.对于你真正想要的URL编码encodeURLComponent)的问题; 遍布整个地方的未转动的&符号; 并且缺少从PHP输出的值的HTML和URL编码,可能导致跨站点脚本风险.

在HTML内部的一个JavaScript字符串文字内的URL内部的URL组件中写入一个值是完全疯狂的,所以毫无疑问会有错误.让我们试着为这种疯狂带来一些可维护性.将JavaScript和URL创建分解为单独的步骤,以便可以获得转义权限.

function urlencodearray($a) {
    $o= array();
    foreach ($a as $k=>$v)
        array_push($o, rawurlencode($k).'='.rawurlencode($v));
    return implode('&', $o);
}
function h($s) {
    echo htmlspecialchars($s);
}
Run Code Online (Sandbox Code Playgroud)

通过定义这些实用程序功能,然后:

<?php } elseif($_SOMETHING == 1 && $_ANOTHER_THING == 2) { ?>
    <?php
        $lpbase= 'http://server.iad.liveperson.net/hc/84152841/?';
        $linkurl= $lpbase.urlencodearray(array(
            'cmd'=>'file',
            'file'=>'visitorWantsToChat',
            'site'=>'84152841',
            'byhref'=>'1',
            'skill'=>'somesiteILS',
            'SESSIONVAR!skill'=>'somesiteILS',
            'SESSIONVAR!Management Company'=>$_MGT_NAME,
            'SESSIONVAR!Community'=>$_NAME,
            'SESSIONVAR!Ad%20Source'=>'somesite.com',
            'imageUrl'=>"http://{$_SERVER['SITENAME']}/images/"
        ));
        $imgurl= $lpbase.urlencodearray(array(
            'cmd'=>'repstate',
            'site'=>'84152841',
            'channel'=>'web',
            'ver'=>'1',
            'skill'=>'somesiteILS',
            'imageUrl'=>"http://{$_SERVER['SITENAME']}/images/"
        ));
    ?>

    <div id="caller_tag">
        <a id="_lpChatBtn" target="chat84152841" href="<?php h($url); ?>">
            <img src="<?php h($imgurl); ?>" name="hcIcon" alt="Chat" border="0">
        </a>
        <script type="text/javascript">
            document.getElementById('_lpChatBtn').onclick= function() {
                var url= this.href+'&referrer='+encodeURIComponent(location.href);
                if ('lpAppendVisitorCookies' in window)
                    url= lpAppendVisitorCookies(url);
                if ('lpMTag' in window && 'addFirstPartyCookies' in lpMTag)
                    url= lpMTag.addFirstPartyCookies(url)
                window.open(url, this.target, 'width=475,height=400,resizable=yes');
                return false;
            };
        </script>
    </div>
Run Code Online (Sandbox Code Playgroud)