PHP代码中出现500错误


500 error in PHP code

我正在为我正在工作的网站创建一个登录页面,我可以访问服务器。

我可以加载一些100%有效的页面,但当我向网站添加成员时,它会给我一条错误消息,说:

错误500

sharedweb.unisite.ac.uk页面不工作。sharedweb.unisite.ac.uk当前无法处理此请求。

我不知道为什么。导致此错误的脚本是:

<?php
  // include function files for this application
  require_once('bookmark_fns.php');
  //create short variable names
  $email=$_POST['email'];
  $username=$_POST['username'];
  $passwd=$_POST['passwd'];
  $passwd2=$_POST['passwd2'];
  // start session which may be needed later
  // start it now because it must go before headers
  session_start();
  try   {
    // check forms filled in
    if (!filled_out($_POST)) {
      throw new Exception('You have not filled the form out correctly. Please go back and try again.');
    }
    // email address not valid
    if (!valid_email($email)) {
      throw new Exception('That is not a valid email address.  Please go back and try again.');
    }
    // passwords not the same
    if ($passwd != $passwd2) {
      throw new Exception('The passwords you entered do not match. Please go back and try again.');
    }
    // check password length is ok
    // ok if username truncates, but passwords will get
    // munged if they are too long.
    if (!preg_match('/^(?=.*'d)(?=.*[A-Za-z])[0-9A-Za-z]{6,12}$/)', $passwd)) {
        throw new Exception('Your password must be between 6 and 12 characters inclusive. Please go back and try again.');
    }
    // attempt to register
    // this function can also throw an exception
    register($username, $email, $passwd);
    // register session variable
    $_SESSION['valid_user'] = $username;
    // provide link to members page
    do_html_header('Registration successful');
    echo "Welcome " $_POST["username"];
    echo 'Your registration was successful.  Go to the members page to start setting up your bookmarks!';
    do_html_url('member.php', 'Go to members page');
   // end page
   do_html_footer();
  }
  catch (Exception $e) {
     do_html_header('Warning:');
     echo $e->getMessage();
     do_html_footer();
     exit;
  }
?>

我该怎么解决这个问题?

您的代码中有两个语法错误:

首先,您需要使用.:将字符串与变量连接起来

echo "Welcome " . $_POST["username"];

其次,这里有一个额外的结束括号:

if (!preg_match('/^(?=.*'d)(?=.*[A-Za-z])[0-9A-Za-z]{6,12}$/)', $passwd)) {
    throw new Exception('Your password must be between 6 and 12 characters inclusive. Please go back and try again.');
}

移除额外的支架:

if (!preg_match('/^(?=.*'d)(?=.*[A-Za-z])[0-9A-Za-z]{8,12}$/', $passwd)) {
    throw new Exception('Your password must be between 6 and 12 characters inclusive. Please go back and try again.');
}

关于这个错误:

已弃用:函数ereg()已弃用

PHP手册:

ereg()在PHP 5.3.0中被弃用,在PHP 7.0.0中被删除。

查看这篇文章:不推荐使用:函数ereg()不推荐使用


提示:您应该打开错误报告,将此代码添加到PHP文件的顶部,这将帮助您查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);