开发者

what is wrong with this snippet

<?php } elseif($_SOMETHING == 1 && $_ANOTHER_THING == 2) { ?>
<?php $_NAME = urlencode($_NAME); ?>
<?php $_MGT_N开发者_StackOverflow中文版AME = urlencode($_MGT_NAME); ?>
</div>    
<?php } ?>

I am getting this error expected ';'


The horror. The horror.

Here's the actual error, in the onclick attribute value:

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

That is, there should be a +' instead of ; after the document.location inclusion, and there should be a closing quote after the imageURL inclusion, and referrer is in the wrong place (it should be just before the document.location inclusion.

It also has problems like the use of escape (never use escape. For URL-encoding you actually want encodeURLComponent); the unescaped ampersands all over the place; and the lack of HTML- and URL-encoding of values output from PHP, potentially causing cross-site scripting risks.

Writing a value inside a URL component inside a URL inside a JavaScript string literal inside an attribute value inside HTML is utter insanity so it's no surprise there are mistakes. Let's try to bring some maintainability to this madness. Break out the JavaScript and URL creation into separate steps where getting the escaping right is possible.

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);
}

With these utility functions defined, then:

<?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>


With an unformatted mess like that it's no wonder you can't find the error.

I tried running it through HTML Tidy but it doesn't like anything between the comments.

mesite.com& ='+escape(document.location); imageUrl=<?php print "ht

I'm not good at reading long lines like that but shouldn't this be

mesite.com& ='+escape(document.location) +'imageUrl=<?php print "ht


First of: why are you opening and closing PHP so many times, you could write it like:

<?php
} elseif($_SOMETHING == 1 && $_ANOTHER_THING == 2) {
$_NAME = urlencode($_NAME);
$_MGT_NAME = urlencode($_MGT_NAME);
?>
<div id="caller_tag">
<!-- BEGIN LivePerson Button Code --><a id="_lpChatBtn"    href='http://server.iad.liveperson.net/hc/84152841/?cmd=file&file=visitorWantsToChat&site=84152841&byhref=1&SESSIONVAR!skill=somesiteILS&SESSIONVAR!Management%20Company=<?php print $_MGT_NAME; ?>&SESSIONVAR!Community=<?php print $_NAME; ?>&SESSIONVAR!Ad%20Source=somesite.com&imageUrl=<?php print "http://{$_SERVER['SITENAME']}/images/";?>' target='chat84152841'  onClick="lpButtonCTTUrl = 'http://server.iad.liveperson.net/hc/84152841/?cmd=file&file=visitorWantsToChat&site=84152841&SESSIONVAR!skill=somesiteILS&SESSIONVAR!Management%20Company=<?php print $_MGT_NAME; ?>&SESSIONVAR!Community=<?php print $_NAME; ?>&SESSIONVAR!Ad%20Source=somesite.com& ='+escape(document.location); imageUrl=<?php print "http://{$_SERVER['SITENAME']}/images/";?>&referrer lpButtonCTTUrl = (typeof(lpAppendVisitorCookies) != 'undefined' ? lpAppendVisitorCookies(lpButtonCTTUrl) : lpButtonCTTUrl); lpButtonCTTUrl = ((typeof(lpMTag)!='undefined' && typeof(lpMTag.addFirstPartyCookies)!='undefined')?lpMTag.addFirstPartyCookies(lpButtonCTTUrl):lpButtonCTTUrl);window.open(lpButtonCTTUrl,'chat84152841','width=475,height=400,resizable=yes');return false;" ><img src='http://server.iad.liveperson.net/hc/84152841/?cmd=repstate&site=84152841&channel=web&&ver=1&imageUrl=<?php print "http://{$_SERVER['SITENAME']}/images/";?>&skill=somesiteILS' name='hcIcon' alt='Chat Button' border=0></a><!-- END LivePerson Button code -->
</div>

And also: the error must be somewhere else, I can't see a missing ";" in php in the code you pasted, unless the error is in javascript.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜