Parse Error: Unexpected $

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

Parse Error: Unexpected $

Post by The Merg »

I am trying to use some PHP code in a web page and am getting the error:

Code: Select all

Parse error: parse error, unexpected $ in /home/content/h/o/w/howardmergler/html/contact.php on line 312
when I load the page. Line 312 is the end of the webpage.

The pieces of PHP code that I am using are as follows. I have not altered any of the code except for the paths listed and also a slight modifcation to the table layout in the last piece of code. I have unfortunately not been able to get any response from the original author of the code, but he does use this code successfully on his site.

The very top of the web page:

Code: Select all

<?php
    session_start();
    session_register('majik_string');
    require '/contact/scfconfig.php';	// Get our config
?>
Located in body:

Code: Select all

<?php
    // Web form spammers frequently either leave HTTP_REFERER empty, set it
    // equal to the form's own URL, or make something up out of thin air.
    // We don't do this "is it our own server" if it's blank, as the "is it
    // blank" check will get that one
    $selfChkStr = $_SERVER['PHP_SELF'];
    $serverChkStr = "http://" . $_SERVER['SERVER_NAME'];
    if(($chkFormRefNotBlank && !$_SERVER['HTTP_REFERER']) ||
       ($chkFormRefNotSelf && preg_match("#$selfChkStr$#i", $_SERVER['HTTP_REFERER'])) ||
       ($chkFormRefOwnServer && $_SERVER['HTTP_REFERER'] &&
        !preg_match("#^$serverChkStr#i", $_SERVER['HTTP_REFERER'])))
    {
	// Almost certainly web form spammers - let 'em wait for it 
	sleep(10);
	// Crude, very crude (gracefully "terminate" the page early)
	print("<div align=\"center\"><font color=\"red\">Disallowed HTTP Referer! ("" .
	    $_SERVER['HTTP_REFERER'] . 
	    "")</font></div>");
	print("</body></html>");
	exit;
    }
		
    // if($requireVerify)
	// print("<div align=\"center\"><font color=\"red\">Cookies must be enabled to use this form.</font></div><p />");
?>

Code: Select all

<?php
                    // Get a pseudo-random alpha-numeric string (no zeros and O's)
                    function pseudo_random_string($length) {
	    $string = "";
	    while($length--) {
		for($indx = rand(49, 90);
		    ($indx > 57 && $indx < 65) || $indx == 79;
		    $indx = rand(49, 90))
		    ;
		$string .= chr($indx);
	    }

	    return($string);
	}

	// Read a line from a config file, stripping comments
	// and blank lines
	function read_file_line($fp) {
	    while(($inString = fgets($fp, 2048)) != false) {
		$inString = rtrim(preg_replace('/\s*#.*/', '',
		    $inString));
		if(!empty($inString))
		    break;
	    }

	    return $inString;
	}

	if(!$HTTP_SESSION_VARS['majik_string'])
	    $HTTP_SESSION_VARS['majik_string'] = pseudo_random_string(5);

	// Read the contact list keys and descriptions into hash
	if(($fp = fopen($recipientFile, "r")) == false) {
	     die("Can't open data file '$recipientFile'.\n");
	}
	while($inString = read_file_line($fp)) {
	    list($key, $description, $value) =
		explode(':', $inString);
	    $options[$key] = $description;
	}
	fclose($fp);

	// If we've more than one choice: present a menu
	if(count($options) > 1) {
	    // If we were given a single arg, that'll be the
	    // selected menu item.
	    if(count($_GET) == 1)
	$selected = strtolower(key($_GET));
	    print("<select name=\"whoto\">\n");
	    foreach($options as $key => $description) {
		print("<option ");
		if(strtolower($key) == $selected)
		    print("selected ");
		print("value=\"" . trim($key) .  "\">" .
		       trim($description) . "\n");
	    }
	    print("</select>\n");
	} else {
	    // There'll be only one...
	    foreach($options as $key => $description) {
		print("<input type=\"hidden\" name=\"whoto\" value=\"" .
		       trim($key) . "\">" . trim($description) . "\n");
	    }
	}

	// Used by the form processor acknowledgment to create a
	// "take me back" link.
	if(!empty($_SERVER['HTTP_REFERER'])) {
	    print("<input type=\"hidden\" name=\"orig_referer\" value=\"" .
		   $_SERVER['HTTP_REFERER'] . "\">\n");
	}
?>

Located in a table in the body:

Code: Select all

<?php
   // Are we requiring CAPTCHA-style "is a human" verification?
   if($requireVerify) {
print <<<End_Of_Data
    <tr><td>&nbsp;</td></tr>
    <tr>
	<td colspan="2">
	    Please enter the verification string on the right into the box on the left.
	</td>
    </tr>
    <tr>
	<td align="right">
	    Verification:
	</td>
	<td align="left">
	    <input type="text" name="verify" size=10>
	    <img src="/contact/scfgenimg.php" width="60" height="20" align="top" alt="Verification string image"/>
	</td>
    </tr>
End_Of_Data;
   }
?>

Thanks in advance,
The Merg
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It is difficult to see what the problem might be with just pieces of the script posted. Maybe someone else will see it. You might want to edit your post so that others could copy the whole thing into a editor to find the problem.
(#10850)
User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

Post by The Merg »

Those are the 4 PHP scripts located in my web page. The rest of the page is regular HTML code. Do you want me to post the entire page?

- Merg
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

The Merg wrote:when I load the page. Line 312 is the end of the webpage.
Line 312 of the file contact.php

Not what the php outputs to the browser (Html).
User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

Post by The Merg »

Zoxive wrote:
The Merg wrote:when I load the page. Line 312 is the end of the webpage.
Line 312 of the file contact.php

Not what the php outputs to the browser (Html).
Right. Line 312 is the last line of contact.php. The contact.php file is a combination of html and PHP code. The PHP code that is contained in the page, is what I displayed in my first post.

Line 312 contains the following:

Code: Select all

</html>
If anything, what kind of thing am I looking for that might result in the error that I am receiving?

- Merg
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

What is the code all the way to the ?> (and throw in, say, four more lines) above that line?
User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

Post by The Merg »

Ambush Commander wrote:What is the code all the way to the ?> (and throw in, say, four more lines) above that line?
I'm not sure I'm following you. I've included the full sets of PHP code that are in my page.

The page in question is http://www.themerg.net/contact.php.

On the page that is on the server, I've also included the PHP code in comments so that when the source is viewed you can see where the PHP code is located.

HTH in troubleshooting,
Merg
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Re: Parse Error: Unexpected $

Post by phpBuddy »

The Merg wrote:I am trying to use some PHP code in a web page and am getting the error:

Code: Select all

Parse error: parse error, unexpected $ in /home/content/h/o/w/howardmergler/html/contact.php on line 312
when I load the page. Line 312 is the end of the webpage.
I would change this

Code: Select all

if(!$HTTP_SESSION_VARS['majik_string'])
            $HTTP_SESSION_VARS['majik_string'] = pseudo_random_string(5);
to this

Code: Select all

if(!$_SESSION['majik_string'])
            $_SESSION['majik_string'] = pseudo_random_string(5);
Now I am not totally sure this will solve your problem.

Besides, I get no 'Parse error' when visit your contact page .... :roll: :roll:

And of course use

Code: Select all

error_reporting(E_ALL);
for debugging.
:)
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post by phpBuddy »

Code: Select all

while($length--) {
for($indx=rand(49,90); ($indx>57&&$indx<65)||$indx==79; $indx=rand(49,90));
    $string .= chr($indx);
}
you have an 'extra' semicolon in end of the for() statement
$indx==79; $indx=rand(49,90)); <- here
User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

Re: Parse Error: Unexpected $

Post by The Merg »

phpBuddy wrote:
The Merg wrote:I am trying to use some PHP code in a web page and am getting the error:

Code: Select all

Parse error: parse error, unexpected $ in /home/content/h/o/w/howardmergler/html/contact.php on line 312
when I load the page. Line 312 is the end of the webpage.
I would change this

Code: Select all

if(!$HTTP_SESSION_VARS['majik_string'])
            $HTTP_SESSION_VARS['majik_string'] = pseudo_random_string(5);
to this

Code: Select all

if(!$_SESSION['majik_string'])
            $_SESSION['majik_string'] = pseudo_random_string(5);
Now I am not totally sure this will solve your problem.

Besides, I get no 'Parse error' when visit your contact page .... :roll: :roll:

And of course use

Code: Select all

error_reporting(E_ALL);
for debugging.
:)
Okay, I took out the extra semi-colon as specified in your second post and changed the HTTP_SESSION_VARS to just _SESSION with no luck.

As for not getting the parse error, I removed the PHP so that you could see the page and where the PHP code was supposed to be. To see the parse error, you can go to http://www.themerg.net/contact_copy.php

Thanks,
Merg
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post by phpBuddy »

:D
Yeah, i see the parse error :D
I understand why you do not have it in your other page, as it can not give any clue.

I have copied your code into my editor.
I will try to find out what it is.

Can you tell excactly where is 'line 3xx'. That is the line number where error is.
Very often the REAL error is in the line before!
It is not here, is it:

Code: Select all

</tr>
End_Of_Data;
   }
Error says unexpected $,
So I would try to find something right before such a char.

I dont know if can be in this preg_match (the end of line: $)

Code: Select all

preg_match("#$selfChkStr$#i",
Each variable begins with '$'
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post by phpBuddy »

I should also have a close look into:
/contact/scfgenimg.php

If I guess, it is a php script for generating a CAPTCHA image
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Code: Select all

print <<<End_Of_Data
should be

Code: Select all

print <<< End_Of_Data
(Change is space after <<<).
Also make sure you have no extra spaces after the starting End_Of_Data.
User avatar
The Merg
Forum Newbie
Posts: 15
Joined: Tue Nov 06, 2007 5:38 pm
Location: Virginia

Post by The Merg »

phpBuddy wrote::D
Can you tell excactly where is 'line 3xx'. That is the line number where error is.
Very often the REAL error is in the line before!
It is not here, is it:

Code: Select all

</tr>
End_Of_Data;
   }
The line number listed is the last line in the page and only contains:

Code: Select all

</html>
phpBuddy wrote: Error says unexpected $,
So I would try to find something right before such a char.

I dont know if can be in this preg_match (the end of line: $)

Code: Select all

preg_match("#$selfChkStr$#i",
Each variable begins with '$'
The page can be seen working with minimal HTML at http://www.themerg.net/contact/scform.php. That is the original page as distributed by the author. As I stated, I haven't changed his code, I just moved it into my web page so that it looks like it is part of my site.

Thanks,
Merg
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post by phpBuddy »

I may have found it.
Let's see.

You are using SCForm, Simple Contact Form. I guess version 1.2.4 (but there is also v-1.2.5.
Anyway, in both these versions, there is a file: scformproc.php
with these lines, in 2 places, at lines:
313-320 and 466-473
( compare with your error: 312 )

Code: Select all

if(PHP_OS == "WIN32" || PHP_OS == "WINNT") {
	    // It seems we only need do this with the "additional headers,"
	    // but we're set up here to easily add other mail() variables.
	    foreach (array('addlHeaders') as $foo) {
		$$foo = preg_replace("/\n$/", "", $$foo);
		$$foo = preg_replace("/\n/", "\r\n", $$foo);
	    }
	}
You see the strange variable names: $$foo
I am sure it must be $foo
..... parse error, unexpected $
Post Reply