Parse error: syntax error, unexpected $end

  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

Code: [ Select ]
<?php session_start();
$name=$_POST["name"];
echo $name;
$password=$_POST["password"];
$link=@mysql_connect('localhost','root');

if($link==TRUE)
{
    
    $dbsel=@mysql_select_db('customer');
    
    if($dbsel==TRUE)
    {
            $query="select * from registration where password='$password' and name='$name'";
            
            $result=@mysql_query($query);
            
            $num_rows =mysql_num_rows($result);
                
            if($num_rows>0)
              {
                
                $_SESSION["name"]=$name;
                
                     /*?>
           <script type="text/javascript">
window.location = "index.php"

</script>

          <?     */
              }
            else
                {
                    echo $name;
                    echo "<script>alert('You have entered the validation code incorrectly, please try again.')</script>";
                    
                    exit();
                    
                    /*?>
           <script type="text/javascript">
           history.back();
                     </script>
          <? */
                }
                
                
    }    
        
}
else
{
    echo "Connection failed";
}
?>
  1. <?php session_start();
  2. $name=$_POST["name"];
  3. echo $name;
  4. $password=$_POST["password"];
  5. $link=@mysql_connect('localhost','root');
  6. if($link==TRUE)
  7. {
  8.     
  9.     $dbsel=@mysql_select_db('customer');
  10.     
  11.     if($dbsel==TRUE)
  12.     {
  13.             $query="select * from registration where password='$password' and name='$name'";
  14.             
  15.             $result=@mysql_query($query);
  16.             
  17.             $num_rows =mysql_num_rows($result);
  18.                 
  19.             if($num_rows>0)
  20.               {
  21.                 
  22.                 $_SESSION["name"]=$name;
  23.                 
  24.                      /*?>
  25.            <script type="text/javascript">
  26. window.location = "index.php"
  27. </script>
  28.           <?     */
  29.               }
  30.             else
  31.                 {
  32.                     echo $name;
  33.                     echo "<script>alert('You have entered the validation code incorrectly, please try again.')</script>";
  34.                     
  35.                     exit();
  36.                     
  37.                     /*?>
  38.            <script type="text/javascript">
  39.            history.back();
  40.                      </script>
  41.           <? */
  42.                 }
  43.                 
  44.                 
  45.     }    
  46.         
  47. }
  48. else
  49. {
  50.     echo "Connection failed";
  51. }
  52. ?>


The above code I am running with xamp. When ever I am removing the block (/**/) from the java script it is giving me error "Parse error: syntax error, unexpected $end in N:\PHP all\xampp\htdocs\Login2\show.php on line 55". But when my friend is running this code in his machine it is running perfectly well and good... Can any one help me with this issue. I dont know is there any particular place for php help. So I am sending it here.
Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

After line 55 try hitting the return key one more time (probably won't fix it but it may, as it has in weird circumstances in the past for me). The most likely issue here is that there are typically 3 formats that text files are saved in: Windows Format, Unix Format, or Mac Format. The differences between the formats are how they end each line. For instance Windows typically uses the sequence of a carriage return and linefeed (CR/LF), Unix typically uses just a linefeed (LF) to end a line, and Macintosh uses just a carriage return (CR) to end a line. This can cause weird and unexpected error messages that are hard to diagnose if you do not know about this. Wikipedia has an excellent article on Newlines that you might want to read about. I will quote a few of the important parts I think that might help explain what I think is causing your situation:

Quote:
The different newline conventions often cause text files that have been transferred between systems of different types to be displayed incorrectly. For example, files originating on Unix or Apple Macintosh systems may appear as a single long line on some Windows programs. Conversely, when viewing a file originating from a Windows computer on a Unix system, the extra CR may be displayed as ^M at the end of each line or as a second line break.

The problem can be hard to spot if some programs handle the foreign newlines properly while others do not. For example, a compiler may fail with obscure syntax errors even though the source file looks correct when displayed on the console or in an editor.


I am not positive, but I have a hunch this is what is going on with you. Let us know what you find out :)
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

When I am hitting a return key it is always giving me the error at the last line and the error is same. Another thing is we both using same O.S.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Compare the values of the php configuration option short_open_tag from both servers. On the server that gives you the error, the value is probably "off" and it's probably "on" for the server where it does work.
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

No....actually my other php programs which are without java script they are running fine....I am using dreamweaver 8 for coading....
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Seriously, I can reproduce the problem you describe by toggling the value of short_open_tag.

Here, read the manual entry for that configuration option.
http://php.net/manual/en/ini.core.php#i ... t-open-tag
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

joebert wrote:
Seriously, I can reproduce the problem you describe by toggling the value of short_open_tag.



I have took u seriously man. :D :D ..That portion is ok, I have chalked it..Thanks for your valuable opinion...But it is not solved. :(
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

Try the following, I cleaned up your code for you a bit ... Quite a bit, just read my comment so you can replace it with the correct value.
PHP Code: [ Select ]
<?php
  session_start();
  $link = mysql_connect('localhost','root') or die('Connection Failed');
  mysql_select_db('customer',$link) or die('Could not connect to database');
  $query = "select * from registration where password='{$_POST['password']}' and name='{$_POST['name']}'";
  $result = mysql_query($query);
  if(mysql_num_rows($result)>0){
    $_SESSION['name']=$_POST['name'];
    header('Location: /index.php');
    exit();
  }
  else{
    echo '<script>alert("You have entered the validation code incorrectly, please try again.")</script>';
    header('Location: /login.php'); //or whatever the previous page is - this for me is better than the javascript appoach
    exit();
  }
?>
  1. <?php
  2.   session_start();
  3.   $link = mysql_connect('localhost','root') or die('Connection Failed');
  4.   mysql_select_db('customer',$link) or die('Could not connect to database');
  5.   $query = "select * from registration where password='{$_POST['password']}' and name='{$_POST['name']}'";
  6.   $result = mysql_query($query);
  7.   if(mysql_num_rows($result)>0){
  8.     $_SESSION['name']=$_POST['name'];
  9.     header('Location: /index.php');
  10.     exit();
  11.   }
  12.   else{
  13.     echo '<script>alert("You have entered the validation code incorrectly, please try again.")</script>';
  14.     header('Location: /login.php'); //or whatever the previous page is - this for me is better than the javascript appoach
  15.     exit();
  16.   }
  17. ?>


Also, where is your password for the mysql_connect function?
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

Actually above code is running only with Php....But I need to run the /*.. .*/ Part of script also...Which is a Java script...It is not running with the Java script which I have quoted. I need to run the Java script also with the Php code..
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

That's where you're incorrect sir ... you don't need the javascript at all ... I have replaced your javascript with the PHP equivalent ...

For example:

JAVASCRIPT Code: [ Select ]
<script type="text/javascript">
  document.location = '/index.php';
</script>
  1. <script type="text/javascript">
  2.   document.location = '/index.php';
  3. </script>


is the same as:

PHP Code: [ Select ]
<?php
  header('Location: /index.php');
  exit();
?>
  1. <?php
  2.   header('Location: /index.php');
  3.   exit();
  4. ?>
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

ok I got it..Thanks a lot....
But can you replace these also
Code: [ Select ]
<script type="text/javascript">
      history.back();</script>
  1. <script type="text/javascript">
  2.       history.back();</script>

This will take back the user to the login page again if it finds the information is not right.
I am a beginner in Php. Thats why if I ask any stupid questing don't mind. :roll:
Thanks again for the help. :D :D
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

I have alread7y replaced it for you ... You can also show the error more cleverly ... for that I will need to see your login page's code though so I can show you where to change it ...

PHP Code: [ Select ]
<?php
  session_start();
  $link = mysql_connect('localhost','root') or die('Connection Failed');
  mysql_select_db('customer',$link) or die('Could not connect to database');
  $query = "select * from registration where password='{$_POST['password']}' and name='{$_POST['name']}'";
  $result = mysql_query($query);
  if(mysql_num_rows($result)>0){
    $_SESSION['name']=$_POST['name'];
    header('Location: /index.php');
    exit();
  }
  else{
    echo '<script>alert("You have entered the validation code incorrectly, please try again.")</script>'; //here you can add some better code ...
    header('Location: /login.php'); //or whatever the previous page is - this for me is better than the javascript appoach
    exit();
  }
?>
  1. <?php
  2.   session_start();
  3.   $link = mysql_connect('localhost','root') or die('Connection Failed');
  4.   mysql_select_db('customer',$link) or die('Could not connect to database');
  5.   $query = "select * from registration where password='{$_POST['password']}' and name='{$_POST['name']}'";
  6.   $result = mysql_query($query);
  7.   if(mysql_num_rows($result)>0){
  8.     $_SESSION['name']=$_POST['name'];
  9.     header('Location: /index.php');
  10.     exit();
  11.   }
  12.   else{
  13.     echo '<script>alert("You have entered the validation code incorrectly, please try again.")</script>'; //here you can add some better code ...
  14.     header('Location: /login.php'); //or whatever the previous page is - this for me is better than the javascript appoach
  15.     exit();
  16.   }
  17. ?>


And no I am glad when people ask questions and learn something from it, that's one of the only ways to learn if you don't know what to look for on google ...
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

You guys are learning that instead of figuring out what the problem was to begin with, to just spend a bunch of time working around it.

If you looked at the short_open_tag directive and didn't fix your error, then you did something wrong. I executed the code over here before I made my suggestion.

I can execute the code you posted, with the comments around the Javascript removed, and short_open_tag turned OFF and I get the error. As soon as I turn short_open_tag ON the error goes away.

Chances are that you either are not allowed to alter the PHP directives with your host and aren't saying anything about that, or you tried to change it and never checked to see if the change was actually being applied by executing phpinfo() within the same file you were testing.

The bottom line is that the problem, the original problem in this thread, was the setting of short_open_tag and the fact that although the first <?php token included the php part, subsequent tokens did not, they were short open tags AKA <?.

If you're having a problem deciphering that directive, try replacing all of your occurrences of plain <? with <?php and watch your code magically quit giving you that "unexpected $end" error.

If you want to waste you, or your employers time *peach* around with workarounds that don't address the original problem and not learn anything, fine. You're not going to sit here and toss my answer, which was the correct answer, away because you don't know what you're doing though. :D
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

joebert wrote:
If you're having a problem deciphering that directive, try replacing all of your occurrences of plain <? with <?php and watch your code magically quit giving you that "unexpected $end" error.
:D

You are right. If I use <?php in place of <? it is running. Thanks joebert :D

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 77 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.