0

I'm trying to make a login page for a website that I'm working on, I have access to the server.

I can load some pages that work 100%, but when I go to add a member to the site, it gives me an error message saying:

Error 500

The sharedweb.unisite.ac.uk page isn’t working. sharedweb.unisite.ac.uk is currently unable to handle this request.

and I don't know why. The script that's causing this error is:

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

How can I fix this?

Panda
  • 6,955
  • 6
  • 40
  • 55

1 Answers1

1

There's 2 syntax errors in your code:

Firstly, you need to concat string with a variable using .:

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

Secondly, there's an extra closing bracket here:

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

Remove the extra bracket:

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

As for this error:

Deprecated: Function ereg() is deprecated

PHP Manual:

ereg() was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

Check out this post: Deprecated: Function ereg() is deprecated


Tip: You should turn on Error Reporting by adding this code to the top of your PHP files which will assist you in finding errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
Community
  • 1
  • 1
Panda
  • 6,955
  • 6
  • 40
  • 55
  • Thanks for the help! I've managed to get a page displayed now. However now, with the error reporting turned on, I have `Undefined index` for the email on line 8, username on line 9, password on line 10, and password2 on line 11 and Function ereg() is deprecated on line 15 –  Apr 18 '16 at 14:40
  • @johnsmith This means that there's something wrong with the form that values are not submitted, maybe also post your form code? – Panda Apr 18 '16 at 14:41
  • I went back and resubmitted the form, and I have got `Deprecated: Function ereg() is deprecated` on line 15 and `Warning: preg_match(): Unknown modifier ')'` on line 34 but when I take that out, I get the `error 500` message again –  Apr 18 '16 at 14:45
  • @johnsmith I've updated post to fix the `preg_match()` error – Panda Apr 18 '16 at 14:50
  • @johnsmith You're welcome! Maybe accept this post by clicking on the tick mark below the upvote/ downvote buttons if it helped you, thanks :) – Panda Apr 18 '16 at 14:54