PHP 错误问题集合

1.包围$pswd1和$pswd2的单引号不可删除,否则无法向MySQL中插入数据。

<?php
$con=mysql_connect("localhost","root","00001111");
if(!$con){
    die("Could not connect MySQL database!");
}
mysql_select_db("sql_test",$con);
$pswd1=md5("0000");
$pswd2=md5("1111");
$sql="INSERT INTO logins (username,pswd)
VALUES
('John','$pswd1'),
('Sam','$pswd2')";
mysql_query($sql,$con);
mysql_close($con);
?>

2.Linux:文件中的换行符为“\n”;Windows:文件中的换行符为“\r\n”;

示例:基于文件的身份验证

文件authenticationFile.txt的内容如下:

jack:ae2bac2e4b4da805d01b2952d7e35ba4
milk:ae2bac2e4b4da805d01b2952d7e35ba4

<?php
$authorized=false;
if(isset($_SERVER['PHP_AUTH_USER'])&&isset($_SERVER['PHP_AUTH_PW'])){
    $authFile=file("authenticationFile.txt");
    if(in_array($_SERVER['PHP_AUTH_USER'].":"
        .md5($_SERVER['PHP_AUTH_PW'])."\n",$authFile)){
        $authorized=true;
    }
}
if(!$authorized){
    header('WWW-Authenticate:Basic Realm="Secret Stash"');
    header('HTTP/1.0 401 Unauthorized');
    print('You must provide the proper credentials');
    exit;
}
?>
如果是在Windows环境下,上述代码中的“\n”需要改为“\r\n”

3.通过fsockopen()在端口80上与www.example.com建立连接,同时输出索引页面:

<?php
$http=fsockopen("www.example.com",80);
$req="GET / HTTP/1.1\r\n";
$req.="Host:www.example.com\r\n";
$req.="Connection:Close\r\n\r\n";
fputs($http,$req);
while(!feof($http)){
    echo fgets($http,1024);
}
fclose($http);
?>
注意($req="GET / HTTP/1.1\r\n";)中的空格!

你可能感兴趣的:(PHP)