douxi0098 2017-01-19 19:47
浏览 61

网站上传后错误500(没有.htaccess,没有WP)[重复]

This question already has an answer here:

I just went to upload the 1st version of a new project I built on the host. To be clear, everything were ready to start working but finally all I got was :

HTTP ERROR 500

I know what does this mean.

My project is 100% handmade with PHP, no WP or other CMS/platform, and there is no Apache file (.htaccess will be added later).
But there is a error_log file, which always have the same line :

[19-Jan-2017 12:59:06 America/Chicago]
PHP Parse error:  syntax error, unexpected '$sgbd' (T_VARIABLE)
in /path/to/the/file.php on line 1

It refers to my database connexion file :

<?php
$sgbd = "mysql";
$host = "localhost";
$database = "dont_worry";
$charset = "utf8";
$user = "this_is_not";
$password = "my_infos";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
];
try {
    $bdd = new PDO('' . $sgbd . ':host=' . $host . ';dbname=' . $database . ';',
'' . $user . '', '' . $password . '', $options);
} catch (Exception $e) {
    die('Erreur : ' . $e->getMessage());
}
?>

This is the 1st file called by my pages, right after session_start();. I really don't get what can be wrong.
I was not prepared to this, thanks for helping !

EDIT : I "solved" the problem, but don't know how it happened. The file was automatically modified on upload, deleting the space between the begining of the PHP code and my first variable :

1 | <?php$sgbd = "mysql";

I verified this file and modified it many times to try to find a solution. The only way was to delete and re-upload the complete project.

</div>
  • 写回答

1条回答 默认 最新

  • dongzou3751 2017-01-19 19:56
    关注

    There's no syntax error in the quoted source with the exception that if you are running this code on PHP 5.3 or earlier, then this:

    $options = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
    ];
    

    will cause syntax error as this short array syntax is available since 5.4.

    Aside from the above, the following line:

    $bdd = new PDO('' . $sgbd . ':host=' . $host . ';dbname=' . $database . ';',
                   '' . $user . '', '' . $password . '', $options);
    

    is over-engeneered. There's absolutely no point of doing '' you typed a lot as this simply equals to empty string. Second row also shows that you got problem understanding the difference between variable and variable's value. This should be easily written as:

    $bdd = new PDO("{$sgbd}:host={$host};dbname={$database};", $user, $password, $options);
    

    Aside from the above it should not be the cause of 500 you are facing.

    EDIT

    If you can invoke PHP from command line (on most linuxes it comes as separate package (i.e. php-cli so you may need to install it first)), then you can check the syntax of your file using -l switch:

    $ php -l file.php
    

    if there's any issue, then you will see the error message. If you see just Errors parsing file.php but no details, add -d error_reporting=E_ALL to the argument list:

    $ php -d error_reporting=E_ALL -l file.php
    

    Alternatively, you can simply try to access your file directly in browser (if it is not in document root, just copy it to make it available - it does not matter calling it alone makes no sense - you just need to ensure you are looking at right file - if you do, then syntax error will be logged. If this is wrong file, then nothing should happen really.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)