Page 1 of 1

Having parse error

Posted: Mon Aug 16, 2010 3:26 am
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. :(

Re: Having parse error

Posted: Mon Aug 16, 2010 4:01 am
by internet-solution
Whats the error message saying?

Re: Having parse error

Posted: Mon Aug 16, 2010 4:45 am
by bluearrow
parse error: parse error in D:\web\e3n.info\log-up.php on line 24

Re: Having parse error

Posted: Mon Aug 16, 2010 12:15 pm
by bluearrow
Anyone ??

Re: Having parse error

Posted: Mon Aug 16, 2010 12:47 pm
by liamallan
try puting both includes at top

Re: Having parse error

Posted: Mon Aug 16, 2010 1:04 pm
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.

Re: Having parse error

Posted: Mon Aug 16, 2010 1:49 pm
by shawngoldw
maybe you need to use <?php instead of <?
...maybe?

Shawn

Re: Having parse error

Posted: Mon Aug 16, 2010 2:13 pm
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. :(

Re: Having parse error

Posted: Tue Aug 17, 2010 10:18 pm
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);

 ?>