开发者

PHP codes works in ver. 5.2, not in 5.3

开发者 https://www.devze.com 2023-03-30 14:04 出处:网络
Is there any way to rewrite the following code so that it works in an environment running PHP version 5.2.x? It works just fine running in version 5.3.5 (dev), but my shared hosting environment (prod)

Is there any way to rewrite the following code so that it works in an environment running PHP version 5.2.x? It works just fine running in version 5.3.5 (dev), but my shared hosting environment (prod) runs 5.2 and throws this error:

Parse error: syntax error, unexpected T_FUNCTION in /home/myperf7/ public_html/inc/store-address.php on line 1

Here's the offending code:

<?php

function storeAddress(){

    // Validation
    if(!$_GET['email']){ return "No email address provided"; } 

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
        return "Email address is invalid"; 
    }

    require_once('MCAPI.class.php');
    // grab an API Key from http://admin.mailchimp.com/account/api/
    $api = new MCAPI('XXXXXXX');

    // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
    // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
    $list_id = "XXXXXXX";

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!   
        return 'Success! Check your email to confirm sign up.';
    }else{
        // An error ocurred, return error message   
        return 'Error: ' . $api->errorM开发者_如何学Pythonessage;
    }

}

// If being called via ajax, autorun the function
if(isset($_GET['ajax'])){ echo storeAddress(); }

?>

And this is how the store-address.php file is being included from my index.php page:

<form id="signup" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    <input type="text" value="" name="email" class="email" id="email" placeholder="email address" required="required" />
    <div class="clear">
        <input type="submit" value="Notify me" name="subscribe" class="button"/>
    </div>
    <span id="response"><?php require_once('inc/store-address.php'); if(isset($_GET['submit'])){ echo storeAddress(); } ?></span>
</form>

Thanks so so so so much!


The problem occurred because Netbeans saved my PHP files all in a single line, a problem I didn't pick up on until I opened my code in plain old Notepad. Even when opening a Netbeans-saved file in Notepad++, the code appeared indented and line-broken as normal. But in classic Notepad all the code is in a single line, meaning the script broke as soon as it reached the first "//" comment.

Thanks everyone for your help!


It seems like you may have some non-whitespace character before your function.

Your PHP code seems valid for 5.2 and 5.3.


The only thing I can see that could be wrong is the $api->errorMessage. Try taking out the whole of that if statement:

if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
    // It worked!   
    return 'Success! Check your email to confirm sign up.';
}else{
    // An error ocurred, return error message   
    return 'Error: ' . $api->errorMessage;
}

And see if you get an error.

0

精彩评论

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

关注公众号