Having parse error

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

Post Reply
bluearrow
Forum Newbie
Posts: 8
Joined: Sun Aug 15, 2010 8:35 am

Having parse error

Post by bluearrow »

I'm working on this simple login script.

Code: Select all

<?
 // Connects to Database 
 include("../base.php");
 
 //checks cookies
 if(isset($_COOKIE['ID_my_site'])){ 
 
 	$username = $_COOKIE['ID_my_site']; 
 	$pass = $_COOKIE['Key_my_site']; 
 	
	$check = mysql_query("SELECT * FROM records WHERE user = '$username'")or die(mysql_error()); 
 	
	while($info = mysql_fetch_array( $check )){ 
 
        //if the cookie has the wrong password, they are taken to the login page 
 		if ($pass != $info['word']){
			
			header("Location: ../login.php");
			
			 
         
 	    }else{ 
		
?>
	
HTML CODE HERE

<?
//------------------------------

		}		}  

          }else{ 
 
          //if the cookie does not exist, they are taken to the login
			 
          header("Location: ../login.php"); 
		  
           } 
?>

I want to put php code into 2 include files so I can change things in one place. Like this

<? include("../log-up.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
<? include("../log-down.php"); ?>


But when I try this It gives me a Parse error. :(
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Having parse error

Post by internet-solution »

Whats the error message saying?
bluearrow
Forum Newbie
Posts: 8
Joined: Sun Aug 15, 2010 8:35 am

Re: Having parse error

Post by bluearrow »

parse error: parse error in D:\web\e3n.info\log-up.php on line 24
bluearrow
Forum Newbie
Posts: 8
Joined: Sun Aug 15, 2010 8:35 am

Re: Having parse error

Post by bluearrow »

Anyone ??
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: Having parse error

Post by liamallan »

try puting both includes at top
bluearrow
Forum Newbie
Posts: 8
Joined: Sun Aug 15, 2010 8:35 am

Re: Having parse error

Post by bluearrow »

liamallan wrote:try puting both includes at top
Tried but still getting the same error.

When I put both parts of code into same page it works fine.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Having parse error

Post by shawngoldw »

maybe you need to use <?php instead of <?
...maybe?

Shawn
bluearrow
Forum Newbie
Posts: 8
Joined: Sun Aug 15, 2010 8:35 am

Re: Having parse error

Post by bluearrow »

shawngoldw wrote:maybe you need to use <?php instead of <?
...maybe?
Shawn
Thanks for suggestion Shawn,

<? should work without a problem. I checked this. didn't work. :(
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Having parse error

Post by internet-solution »

It looks like PHP parser does not like splitting for ... each loop into two files. When its parsing log-up.php, it cannot find right number of '}'s and hence the parse error. You can achieve your objective by using a function in login.php, which takes html code as an argument:

Code: Select all

<?php
function login($htmlCode){
 // Connects to Database
 include("../base.php");
 
 //checks cookies
 if(isset($_COOKIE['ID_my_site'])){
 
        $username = $_COOKIE['ID_my_site'];
        $pass = $_COOKIE['Key_my_site'];
       
        $check = mysql_query("SELECT * FROM records WHERE user = '$username'")or die(mysql_error());
       
        while($info = mysql_fetch_array( $check )){
 
        //if the cookie has the wrong password, they are taken to the login page
                if ($pass != $info['word']){
                       
                        header("Location: ../login.php");
            }else{
                    echo $htmlCode;
//------------------------------
                }               }  

          }else{
 
          //if the cookie does not exist, they are taken to the login
                         
          header("Location: ../login.php");
                 
           }
}
?>
Then call this function from other files:

Code: Select all

<?php
include("../login.php"); 
$thisPageHtmlCode='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>';
login($thisPageHtmlCode);

 ?>
Post Reply