cactus Posted March 15, 2011 Share Posted March 15, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/ Share on other sites More sharing options...
renoirb Posted March 15, 2011 Share Posted March 15, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187811 Share on other sites More sharing options...
cactus Posted March 15, 2011 Author Share Posted March 15, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187822 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2011 Share Posted March 15, 2011 That is telling me that you don't have a form field with the name of "username". What does your form look like. Please post code between tags. Ken Link to comment https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187824 Share on other sites More sharing options...
cactus Posted March 15, 2011 Author Share Posted March 15, 2011 <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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187826 Share on other sites More sharing options...
aabid Posted March 15, 2011 Share Posted March 15, 2011 For me your script worked and correctly and I checked the variables also....they are showing the correct values. May be there is something else that is causing problem in this i think. Link to comment https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187851 Share on other sites More sharing options...
cactus Posted March 15, 2011 Author Share Posted March 15, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187855 Share on other sites More sharing options...
ChemicalBliss Posted March 15, 2011 Share Posted March 15, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187863 Share on other sites More sharing options...
cactus Posted March 15, 2011 Author Share Posted March 15, 2011 Hi I tried your code and i'm still getting the undefined index username error. Any other ideas? I really appreciate Link to comment https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187886 Share on other sites More sharing options...
ChemicalBliss Posted March 15, 2011 Share Posted March 15, 2011 Read my post more carefully and understand what i told you to do Tell us what the print_r gives. (easier to read in page source) Link to comment https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187888 Share on other sites More sharing options...
cactus Posted March 15, 2011 Author Share Posted March 15, 2011 oh yeh i kinda scanned it lol The print_r thing says Array() I have no idea what this means, can you help? Link to comment https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187933 Share on other sites More sharing options...
ChemicalBliss Posted March 16, 2011 Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1187995 Share on other sites More sharing options...
cactus Posted March 16, 2011 Author Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1188071 Share on other sites More sharing options...
kenrbnsn Posted March 16, 2011 Share Posted March 16, 2011 What is line 58? Ken Link to comment https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1188183 Share on other sites More sharing options...
cactus Posted March 16, 2011 Author Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1188230 Share on other sites More sharing options...
ChemicalBliss Posted March 17, 2011 Share Posted March 17, 2011 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 https://forums.phpfreaks.com/topic/230715-parse-error-syntax-error-unexpected-t_constant_encapsed_string/#findComment-1188739 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.