Jump to content
iwato

Error 500 - Probable Causes

Recommended Posts

BACKGROUND:  The following code was copied from the Grammar Captive mainpage where it performs  just as it was designed to perform.  Sadly, however, it fails on my local test server.  The error log to which the file points indicates nothing and the data is properly entered into the data base.  Further, although the PHPMailerAutoload.php class loads properly, the mail does not get sent and message is produced.  Finally, the Javascript Console claims the following: "Failed to load resource: the server responded with a status of 500 (Internal Server Error)".  And, the resource that it names is the very same listed below.  I checked the confirmation_mail.php file and discovered that two of the links were not using secure protocol and adjusted these. 

Now, it worked before I upgraded to PHP 5.6.3, but for some reason it does not work now.

<?php
	ini_set('display_errors', 0);
	ini_set('error_log', '../../../error.log');
	ini_set('log_errors', 1);
	error_reporting(E_ALL);
	require_once './php_mailer/PHPMailerAutoload.php';
	$name = $email = $location = $language_level = $language = $personal = $msg = '' ;
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['location']) && !empty($_POST['language']) && !empty($_POST['language_level']) && isset($_POST['personal'])) {
			$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
			$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL, FILTER_SANITIZE_EMAIL);
			$location = filter_var($_POST['location'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
			$language_level = filter_var($_POST['language_level'], FILTER_SANITIZE_NUMBER_INT, FILTER_VALIDATE_INT);
			$language = filter_var($_POST['language'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
			$personal = filter_var($_POST['personal'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
			include_once('./classes/class.lunarpages.php');
			$lunarpages = new Lunarpages();
			$mysqli_obj = $lunarpages->get_mysqli_obj();
			function random_password() {
				$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!_!_!_!_!_!_!_';
				$pass = array();
				$alphaLength = strlen($alphabet) - 1;
				for ($i = 0; $i < 8; $i++) {
					$n = rand(0, $alphaLength);
					$pass[] = $alphabet[$n];
				}
				return implode($pass);
			}
			$password = random_password();
			$hash = password_hash($password, PASSWORD_DEFAULT);
			$active = 0;
			$newsletter = 1;
			$webinar = 1;
			$tbl_name = 'thege0_grammarcaptive.captive_roster';
			$sql_1 = "INSERT INTO $tbl_name (user_name, language, email_address, location, language_level, personal, psw_hash, active, newsletter, webinar) VALUES ('" . $name . "','" . $language . "','" . $email . "','" . $location . "','" . $language_level . "','" . $personal . "','" . $hash . "','" . $active . "','" . $newsletter . "','" . $webinar . "')"; 
			$mysqli_obj->query($sql_1);
			$mail = new PHPMailer;
			$mail->CharSet = 'UTF-8';
			$mail->isSMTP();
			$mail->SMTPDebug = 0;
			$mail->Debugoutput = 'html';
			$mail->Host = "baha.lunarbreeze.com";
			$mail->Port = 587;
			$mail->SMTPAuth = true;
			$mail->Username = "webinar@grammarcaptive.com";
			$mail->Password = "...";
			$mail->setFrom('webiniar@grammarcaptive.com', 'Grammar Captive');
			$mail->addReplyTo('admin@grammarcaptive.com', 'Grammar Captive Administration');
			$mail->addAddress($email, $name);
			$mail->Subject = 'Account Verification';
			$html_message = file_get_contents('../../confirmation_mail.php');
			$html_message = str_replace('%name%', $name, $html_message);
			$html_message = str_replace('%email%', $email, $html_message);
			$html_message = str_replace('%hash%', $hash, $html_message);
			$html_message = str_replace('%language%', $language, $html_message);
			$mail->msgHTML($html_message);
			$alt_message = "Congratulations,  $name! You have successfully created a Grammar Captive account./n/r Please click on the link below to verify that you are the owner of the account and to receive your first edition of Seven Gates, the Grammar Captive weekly newsletter. You will be notified by email as soon as a date and time have been set for your first free webinar./n/r http://www.grammarcaptive.com/_utilities/php/email_verify.php?name=$name&email=$email&hash=$hash&language=$language&letter_no=1";
			$mail->AltBody = $alt_message;
			if (!$mail->send()) {
				$msg = $mail->ErrorInfo;
			} else {
				$msg = "<h1 style = 'text-align: center;color: #4E7F4E;font-size: 3em;font-weight: bold;font-family: Bradley Hand, cursive;margin: -1em, auto;vertical-align: bottom;'>Free Webinar</h1><p style='text-align: center; margin-top: -1.2em; font-size: 0.9em;'>Including the first edition of the Seven Gates weekly newsletter!</p><h2>Congratulations, <span style='font-family: Bradley Hand, cursive;font-size:1.4em;color:#4E7F4E;'>$name</span>!</h2><p style='text-align:left;'>Your information has been securely sent using SMTP protocol and a TSL/SSL security layer.</p><p>Please look for a verification email in your email inbox and click on the link provided.  This will insure that you are the person who submitted the data.</p><p>You will receive an email in your inbox as soon as a date and time for the free webinar has been scheduled.</p><p>Episode No. 1 of the Seven Gates newsletter will arrive in your email inbox after you have confirmed your identity.</p><p><span style='color:#4E7F4E;font-weight:bold;'>CAUTION: </span>Sometimes the verification mail will be redirected to a user's junk box.  Do not treat it as junk, for without verification no newsletter can be received!</p><p>In addition, you may have to adjust your email software to insure that future email from Grammar Captive arrives in your inbox.</p><p style='text-align:left;'><span style='color:#4E7F4E;font-weight:bold;'>SECURE STORAGE:</span> Your information is stored on the Lunarpages webserver in Orange, California. If you have any questions about the safety of your data, please read under the headings <em>Other/Internet Security</em> and <em>Other/Legal Privacy</em> in the Grammar Captive navigation bar on your left. Alternatively, you may email the Grammar Captive webmaster at <a href='mailto:admin@grammarcaptive.com?subject=Grammar%20Captive%20Webmaster%20-%20Question%20About%20Internet%20Security' title='The Grammar Captive webmaster' target='_blank'>admin@grammarcaptive.com</a>.</p><p>Thank you for your cooperation!</p>";
			}
			echo $msg;
		}			
	}
?>

Any ideas?

Roddy

 

Share this post


Link to post
Share on other sites

What is the output when the code runs?

If you're using AJAX to run the PHP, then you will be able to see the output in the response body of the request in the network tab of the browser development tools.

Share this post


Link to post
Share on other sites

I switched from Safari to Firefox, as I was unable to discover any useful information.  Firefox complained the following;  No headers for this request and pointed to the above file.  Well, the value of $msg contains HTML that depends on the mainpage to provide.  So I added the "TEXT" dataType to my AJAX call, but this did not help.

The AJAX

var dataString = 'name='+ name + '&email=' + email + '&location=' + location + '&language_level=' + language_level + '&language=' + language + '&personal=' + personal;
$.ajax({
    type: "POST",
    url: './_utilities/php/freewebinar_filler_form.php',
    data: dataString,
    dataType: 'TEXT',
    statusCode: {
        404: function() {
        alert( "Page not found" );
    }},
    success: function(message) {
        console.log(message);
        $('#main').html("<div id='message'></div>").css('background-image',"url('./_images/gates/opacity/collage_bg3.png')");
        $('body, html').animate({scrollTop: $('#main').offset().top},800);
        $('#message').html(message)
        .append("<div id='fw_signature'><span style='font-family:Bradley Hand,cursive;font-size:1.4em;color:#4E7F4E;'>Roddy</span>,<br />Your host at Grammar Captive</div>")
        .hide()
        .fadeIn(2000, function() {
            $('#message');
        });
    }
});

Roddy

Edited by iwato

Share this post


Link to post
Share on other sites

A 500 response is just a generic code for an error on the server.  Usually that's an error with PHP, but it could also be an error with the web server itself.  If you don't see any errors in the PHP error log, check the web server's error log.

Share this post


Link to post
Share on other sites

Unfortunately, the Apache access_log merely confirms the 500 error code, and nothing even appears in the php_errors.log and error_log.  Firefox and Safari provide more information.  On the bright side I was able to find and access all three logs and have noted where to find them again easily.

Roddy

Share this post


Link to post
Share on other sites

It wouldn't be the access log, Apache also has an error log.  If there's an error though there will be a message somewhere.

Share this post


Link to post
Share on other sites
1 hour ago, iwato said:

..., and nothing even appears in the php_errors.log and error_log....

Roddy

Share this post


Link to post
Share on other sites

Is there anything in the AJAX response? If it's empty, the only thing left to do is keep stripping out parts of the PHP code until it stops returning 500.

Share this post


Link to post
Share on other sites

If you are absolutely positive that you are looking in the correct places, then you need to figure out why those places don't have the error message you're looking for.  Simple as that.  When I was doing the same thing, I looked at the PHP configuration file to see exactly what it was actually using as the error settings instead of assuming that the XAMPP control panel was opening the right file.  It turns out it was not.  But as long as we're quoting ourselves...

If there's an error though there will be a message somewhere.

Share this post


Link to post
Share on other sites

JSG: Under the assumption that all three of the Apache logs are usually placed in the same folder, I am looking in the right place.  I say this because there are other sources of unrelated error reported in the same log files.  Following your suggestion the PHP info file also reveals the same address.  Alas, there are no error messages other than Error Code 500!

INGOLME:  I rewrote the console.log(message); statement in the AJAX success function as follows:

console.log('This the message that does not display: ' + message);

Even this did not appear.

Roddy

Share this post


Link to post
Share on other sites

OK.  By default my XAMPP installation had the error log commented out from the php.ini file, so it was creating a file called error_log in the same directory as the PHP script.  Did you change the error reporting settings in your code to not print error messages?  Because otherwise, there is going to be an error message somewhere, there always is.

Share this post


Link to post
Share on other sites

Try

var dataString = {
                name: name,
                email: email,
                location_name: location_name,
                language_level: language_level,
                language: language,
                personal: personal
            }

Had to change location, as it was being treated as a reserved keyword, remember to change $_POST['location'] to $_POST['location_name']

Edited by dsonesuk
  • Thanks 1

Share this post


Link to post
Share on other sites
22 minutes ago, iwato said:

INGOLME:  I rewrote the console.log(message); statement in the AJAX success function as follows:


console.log('This the message that does not display: ' + message);

Even this did not appear.

Roddy

You can't put the console.log() in the success function, because a 500 is a failure. This is why you should monitor the AJAX request in the browser's developer tools.

Share this post


Link to post
Share on other sites

DSONESUK:  Yes, I agree.  It seem strange  that an AJAX call of POST type would  accept a query string of GET-type, but this is exactly what it does.  The idea came from StackOverflow, and as I did not know how to write POST-type data, I tried it.  This is surely not the source of the problem.  In retrospect it is worth citing the jQuery manual in this regard -- namely, "An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0.  Apparently, jQuery negotiates the protocol with the PHP file to which the AJAX makes its request.  In any case, thank your for teaching me how to write a POST request manually.  

JSG:  My installation is not a prepackaged stack.  Rather, it is an improvisation of the software that comes with Apple's operating system.  Right now, i am considering about reverting to the old php.ini file, or at least mimicking it as best I can in the new file.  My gosh, I do not have time to redo over 20 PHP files when I am so close to completing the first phase of my development.  I can accommodate Matomo by modifying the online files that function perfectly well in their current environment.  Besides, if everything goes well, I will purchase new equipment and will have to upgrade to PHP 7+ whatever, anyway.

In fact, just responding to your recommendation has convinced me that this is the best approach under the current circumstances.  For the moment, I simply do not have the time to go back and redo every single file to meet the new PHP demands.  There are over 20 files that will likely require some sort of adjustment, and it is already clear that each file will have its own peculiar adjustment needs.

INGOLME:  Yes, I understand, but the browser tells me nothing other than a failed header that my application does not even require, if only I could solve the problem created by the PHP upgrade.

____________________

Alas, I have decided.  I will complete all further modifications of my web-application online until I can afford new equipment and am better positioned to go back and redo everything to meet PHP's new demands.  This is a strategic decision that includes that goes beyond code design.

Roddy

 

 

Edited by iwato

Share this post


Link to post
Share on other sites

JSG:  No, i have never messed with the error reporting of the php.ini file.  At most I have changed dynamically the destination of the error reporting from display to error log.  What is more, contrary to your belief, I do not regularly suppress the display of error settings until after I am sure that I have addressed all warnings, notices, and error messages to the best of my ability.

in an effort to salvage my local server I have requested my web server host, Lunarpages,  to provide me with a copy of their php.ini file so that I can discover what they have changed and better understand how to modify my own php.ini file. In any case, the design flaws that you have pointed out and that have rendered my local server dysfunctional in the new environment will have to be dealt with in the second phase of development -- not this one.  I must begin podcast and newsletter production as quickly as possible.

Roddy

Share this post


Link to post
Share on other sites

I'm not asking you to look at the error console. Look at the network tab. It will have a whole lot of information in it whether or not the request failed. It has the HTTP method, the HTTP status code, the request headers, the request body, the response headers and the response body and it even provides information as to how long the request took to send, process and return from the server.

Please show all the information that the network tab provides about the request.

I've attached an image showing you what the network tab should look like in your browser:

request.png

  • Thanks 1

Share this post


Link to post
Share on other sites
Quote

This is surely not the source of the problem.  In retrospect it is worth citing the jQuery manual in this regard -- namely, "An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0.  Apparently, jQuery negotiates the protocol with the PHP file to which the AJAX makes its request.  In any case, thank your for teaching me how to write a POST request manually. 

Well I put a short script JavaScript and php, together using your provided code, and with your defining of   dataString it just didn't want to know, with mine it went through with only the problem of 'location' conflict ( it tried to go to the url of value supplied), I renamed this and it worked fine, tested several times, worked in every case.

Share this post


Link to post
Share on other sites

Dsonesuk:  What version of PHP are you using?

INGOLME:  Please find attached your requested information for the HTTPRequest error code 500

Roddy

firefox_network_results.tiff

Edited by iwato

Share this post


Link to post
Share on other sites

It would appear PHP is not printing any of the error messages. You're going to have to either find a way to enable error logging on the server or go through the procedure of cutting out code until it starts giving a useful response.

Share this post


Link to post
Share on other sites

I have just received a copy of the stock php.ini file that Lunarpages (my web host server) is using.  Hopefully, it will lend a clue into what is going on and how to adjust for it.

Roddy

Share this post


Link to post
Share on other sites

If you're not getting any error output at all, then it really sounds like error messages aren't being printed for whatever reason.  Search for error_log in php.ini and make sure it is set to a valid filename, and also make sure that whatever user Apache is running under has write permission for that file.

Share this post


Link to post
Share on other sites

The absence of error messages appears to have been created by improper coding errors of the like that creates a blank screen in PHP when you miss a comma, semi-colon, or quotation mark.  

The past two weeks have been a very painful learning experience for me, as it came just as I am approaching the peak of my first phase of site development.  I have staked out a very mountainous range to climb, and it hurt to be so close and unable to reach my first peak on schedule.  I unwisely allowed my hope to overshadow my past experience of disappointment, and was brutally frustrated as a result.  In any case, a long trek around the first peak has allowed me to regain my strength and upward orientation, and I am back into climbing mode.  

Thank you, everyone for your effort.  I apologize for putting you through my frustration.  Hopefully I have learned well from my errors.

Roddy

 

Share this post


Link to post
Share on other sites

The absence of error messages appears to have been created by improper coding errors of the like that creates a blank screen in PHP when you miss a comma, semi-colon, or quotation mark. 

That's a parse or syntax error, when that happens only the settings in php.ini will apply.  When there is a parse error it can't determine the structure of the code so it doesn't execute anything at all, including any commands to set error reporting or other options, so it uses the settings in php.ini.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×