Error in my code

I have looked for the punctuation error (I have read the PHP Common Errors), but I cannot find the cause of the error.

I have also removed anything unneeded, including any comments (I will add them back in after I get this running).

The error is: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in change_password.php on line 49

Line 49 is the $query.


<?php
if (!isset($_SESSION['first_name'])) {
	$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\\\') ) {
    	$url = substr ($url, 0, -1); }
      $url .= '/index.php';
      header("Location: $url");
			} elseif (isset($_POST['submitted'])) { 
					$query = "SELECT email, user_id FROM users_tbl WHERE first_name=($_SESSION['first_name'])";		

I am close to pulling my last hair.

The error is been caused by the following line because you have a random open and closing parenthesis that shouldn’t be there.

$query = "SELECT email, user_id FROM users_tbl WHERE first_name=($_SESSION['first_name'])";

The below code will fix the issue.

$query = "SELECT email, user_id FROM users_tbl WHERE first_name='" . $_SESSION['first_name'] . "'";

I see what you did and it worked. I will have to study the code more when I get back to my computer tonight.

Of course, fixing this gave me a new error (a simple one), and fixing that has given me several errors that I can work on tonight. :slight_smile:

Off Topic:

If it works perfectly the first time, you know you’ve done something wrong :wink:

I have managed to get my change_password to work, thanks to the help here.

Now when I try a different login, I seem to be running into trouble. First, I logout and I suspect there is something wrong with my logout code. When I tried to change the password for a different user, it acted as if the session was still set (I think).

	<div id="main">
	<?php
    // If no first_name variable exists, redirect the user.
	if (!isset($_SESSION['first_name'])) {
		header("Location: http://www.recse.org/clergy/index.php");
		exit(); // Quit the script.
	} else { // Logout the user.
	$_SESSION = array(); // Destroy the variables.
	session_destroy(); // Destroy the session itself.
	setcookie (session_name(), '', time()-300, '/', '', 0); // Destroy the cookie.
	}
	echo '<h3>You are now logged out. </h3><br />
	?>
	</div>

This code is giving me an error of:
Warning: Cannot modify header information - headers already sent by (output started at /logout.php:24) in /homepages/4/d322583329/htdocs/dev/logout.php on line 37

Line 37 is the setcookie line.

The error is occurring because you currently have your PHP code wrapped inside a DIV element which is illegal as for a PHP header() function to work it needs to be before any white space and HTML code otherwise you will get the above error.

Looks like I need to rewrite my logout.php code. I will start with:


<?php
session_start();
if(!isset($_SESSION['first_name'])) { // Not logged in
 include_once('index.php'); // send to index page
 die(); // stop processing
} 

This is code I modified from another thread. StarLion at http://www.sitepoint.com/forums/showthread.php?782440-PHP-Sessions-Works-amp-Doesnt-Other-Days had some code that does not use redirect, so I will give it a run and see what happens.