Jump to content

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING


cactus

Recommended Posts

Hi,

 

I keep getting the above error when testing out my system.

 

The section of code that is having the error is:

 

if ($_SESSION['username']=='login') {

  if (isset($_REQUEST['file'])) {

      $fc = file_get_contents($_REQUEST['file']);

      $text = explode("<!-- EDITABLE -->",$fc);

      echo "<form method='post' action=''><textarea name='content'>$text[1]</textarea>";

      echo "<p><input type='hidden' name='file' value='".$_REQUEST['file']."' /><input name='submitUpdate' type='submit' value='Update Page'></form>";

  }

  else {

      // edit to link to your own static html files

      echo "<p align='center'>

      <a href="?index.html">Home Page</a><br/>

      <a href="?contact_us.html">Contact Us</a><br/> //THIS IS THE LINE THATS GIVING ME THE PROBLEM

      <br/>

      <em>Click on the links above to edit the files.</em><br/>

      <a href="?logout">logout</a></p>";

  }

}

 

 

Can anybody help? I'd really appreciate it.

Thanks in advance :)

Link to comment
Share on other sites

When you do <?php echo ""; ?> you need to NOT HAVE the same quoting type with your html tags.

 

There is three ways to output things, using.

1. Single quote  (')  -- if you put $variables, they will NOT be parsed, speedier.

2. Double quotes (")  -- if you put variables, they WILL be parsed. heavier.

3. HEREDOC -- better for html output.

 

Here is the basic: http://php.net/manual/en/language.types.string.php

 

1. <?php echo 'Hello "world"!'; ?>

2. <?php echo "Hello \"world\""; ?>

3. <?php echo <<<OUTPUT

Hello "world"!

OUTPUT; ?>

 

At 3, see that OUTPUT (the second mention) MUST be the FIRST element on its line.

 

Hope it helps.

Link to comment
Share on other sites

That's fantastic thank you!

 

I have another little problem that I was hoping I could get some help on.

 

When I get the login screen I get the error undefined variable username.

 

This is the code:

 

if (isset($_POST['Submit'])) {

    if (($_POST['username'] == 'admin') && ($_POST['passwd'] == 'xyz')) { //ERROR ON THIS LINE

        $_SESSION['username'] = 'login';

    }

  else {

      echo "<b>Your login details is not correct. Please try again</b>";

    }

}

 

How would I solve this?

Any help is appreciated.

Thanks :)

Link to comment
Share on other sites


<form method="post" action="">
  <table width="400"  border="0" align="center" cellpadding="2" cellspacing="2">
       <tr>
      <td>Username: </td>
      <td><input type="text" name="username"></td>
    </tr>
    <tr>
      <td>Passwd: </td>
      <td><input type="password" name="passwd"></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit">   <input type="reset" name="reset" value="Reset">
</td>
    </tr>
  </table>
</form>

 

This is my form. Thanks a lot :)

Link to comment
Share on other sites

This is my entire code:

 

<?php
session_start();

if (isset($_REQUEST['logout'])) {
session_unset();
}

if (isset($_POST['submitUpdate'])) {
    if (get_magic_quotes_gpc()) { 
        $_POST = array_map('stripslashes',$_POST);
    }
   $fc = file_get_contents($_POST['file']);
   // truncate file
   $fw = fopen($_POST['file'], 'w+');
   $text = explode("<!-- EDITABLE -->",$fc);
   $newText = $text[0]."<!-- EDITABLE -->".htmlentities($_POST['content'])."<!--EDITABLE ->".$text[2];
   if (fwrite($fw, $newText)===FALSE) {
      die("Cannot write to file.");
   } 
   fclose($fw);
   exit("<div><span class='redText'>The file has been updated. Click <a href=\"index.php\">here</a> to go back to admin page.</div>");
}

if (isset($_POST['Submit'])) {
    if (($_POST['username'] == 'admin') && ($_POST['passwd'] == 'xyz')) {
        $_SESSION['username'] = 'login';
    }
   else {
       echo "<b>You login details is not correct. Pls login again</b>";
    }
}

if ($_SESSION['username']=='login') {
   if (isset($_REQUEST['file'])) {
       $fc = file_get_contents($_REQUEST['file']);
       $text = explode("<!-- EDITABLE -->",$fc);
       echo "<form method='post' action=''><textarea name='content'>$text[1]</textarea>";
       echo "<p><input type='hidden' name='file' value='".$_REQUEST['file']."' /><input name='submitUpdate' type='submit' value='Update Page'></form>";
   }
   else {
      // edit to link to your own static html files 
      echo "<p align='center'>
       <a href='body/recentnewsbody.html'>Recent News</a><br/>
       <a href=''>Parents</a><br/>
       <br/>
       <em>Click on the links above to edit the files.</em><br/>
      <a href='staff.php'>logout</a></p>";
   } 
}
session_destroy();
?>

<form method="post" action="">
  <table width="400"  border="0" align="center" cellpadding="2" cellspacing="2">
       <tr>
      <td>Username: </td>
      <td><input type="text" name="username"></td>
    </tr>
    <tr>
      <td>Passwd: </td>
      <td><input type="password" name="passwd"></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit">   <input type="reset" name="reset" value="Reset">
</td>
    </tr>
  </table>
</form>

 

What version of PHP did you test it on?

Link to comment
Share on other sites

Always debug your code so you know that it is working exactly how you want, ie - all the variables should be exactly what you expect them to be.

 

use combinations of echo and print_r calls. eg:

 

<?php
session_start();

print_r($_POST); // This will appear at the top of this page. It will contain every variable passed via a form that used the POST method.

if (isset($_REQUEST['logout'])) {
session_unset();
}

if (isset($_POST['submitUpdate'])) {
    if (get_magic_quotes_gpc()) { 
        $_POST = array_map('stripslashes',$_POST);
    }
   $fc = file_get_contents($_POST['file']);
   // truncate file
   $fw = fopen($_POST['file'], 'w+');
   $text = explode("<!-- EDITABLE -->",$fc);
   $newText = $text[0]."<!-- EDITABLE -->".htmlentities($_POST['content'])."<!--EDITABLE ->".$text[2];
   if (fwrite($fw, $newText)===FALSE) {
      die("Cannot write to file.");
   } 
   fclose($fw);
   exit("<div><span class='redText'>The file has been updated. Click <a href=\"index.php\">here</a> to go back to admin page.</div>");
}

if (isset($_POST['Submit'])) {
    if (($_POST['username'] == 'admin') && ($_POST['passwd'] == 'xyz')) {
        $_SESSION['username'] = 'login';
    }
   else {
       echo "<b>You login details is not correct. Pls login again</b>";
    }
}

if ($_SESSION['username']=='login') {
   if (isset($_REQUEST['file'])) {
       $fc = file_get_contents($_REQUEST['file']);
       $text = explode("<!-- EDITABLE -->",$fc);
       echo "<form method='post' action=''><textarea name='content'>$text[1]</textarea>";
       echo "<p><input type='hidden' name='file' value='".$_REQUEST['file']."' /><input name='submitUpdate' type='submit' value='Update Page'></form>";
   }
   else {
      // edit to link to your own static html files 
      echo "<p align='center'>
       <a href='body/recentnewsbody.html'>Recent News</a><br/>
       <a href=''>Parents</a><br/>
       <br/>
       <em>Click on the links above to edit the files.</em><br/>
      <a href='staff.php'>logout</a></p>";
   } 
}
session_destroy();
?>

<form method="post" action="">
  <table width="400"  border="0" align="center" cellpadding="2" cellspacing="2">
       <tr>
      <td>Username: </td>
      <td><input type="text" name="username"></td>
    </tr>
    <tr>
      <td>Passwd: </td>
      <td><input type="password" name="passwd"></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit">   <input type="reset" name="reset" value="Reset">
</td>
    </tr>
  </table>
</form>

Link to comment
Share on other sites

Is that when you submit data through the form? (the page it gives you after you click submit)

 

If it is, are you using any other files or forms other than the single code you posted above?

 

if not, and you still have a problem, can you give us the exact error copy+paste?

Link to comment
Share on other sites

No thats what it said before I submitted the form.

 

It said this after i've submitted the form:

 

Array ( [username] => admin [passwd] => xyz [submit] => Submit )

 

The error message exactly is:

 

Notice: Undefined index: username in C:\wamp\www\admin.php on line 58

 

Thanks for the help :)

Link to comment
Share on other sites

if (isset($_POST['Submit'])) {
    if (($_POST['username'] == 'admin') && ($_POST['passwd'] == 'xyz')) { //This is line 58
$_SESSION['username'] = 'login';
    }
else {
echo "<b>You login details is not correct. Pls login again</b>";
    }

 

Thanks :)

Link to comment
Share on other sites

Notices are informational, they are not errors as such. They can however point towards a logic error you may have overlooked.

 

Anything like: "Notice: Undefined ..."

This means that what it says (in this case the "index" named "username") does not exist before that point/line.

 

To get rid of the error, you have two options in all of these cases:

1. Declare the index/variable before hand (create it)

2. Check if that variable/index actually exists using "isset()"

 

I would use the latter in this case as the variable needs to come from a user, so a simlpe check will do:

 

In your IF statements for the username and passwd, check if they are set (isset()) BEFORE you check what their value is, and this can be in the same if statement. This is good practice to do this.

 

I am glad you have notices turned on though and good job for wantnig to get rid of them even though (im assuming..) this code works fine.  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.