stickynote427 Posted February 13, 2010 Share Posted February 13, 2010 I have a tutorials page on my site. A parameter in the URL determines the tutorial displayed, and if a "code" parameter is set, then that example for the tutorial will be displayed instead. However, when I try to include() the example file, my entire page does not load; I'm assuming because include() is obviously run in PHP, as well as the file starting out with some PHP code. Here's what I have at the top of my tutorials page: <?php if (isset($_GET['t'])) { //if a tutorial is specified if (isset($_GET['code'])) { //check if the example should be shown include('howto/'.$_GET['t'].'/code'.$_GET['t'].'.txt'); //include the example file } else { //if the example file should not be shown include('howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt'); //include the tutorial file } echo("<a href=\"http://blakechesney.com/p/howto\">Return to the main how-to page</a>"); //echo navigation back to the main page } else { // perform actions related to the main page; probably irrelevant to my problem ?> When the "code" parameter is set, though, the page only loads up to the point until I want to include() the example file. Any ideas? Thanks. stickynote427 Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/ Share on other sites More sharing options...
cwarn23 Posted February 13, 2010 Share Posted February 13, 2010 Try using the require() function as it has better error reporting. Also if you are planning to store the file data in a variable you can alternatively use the file_get_contents() function. The cause - the file does not exist. Be sure the path is relative of the php script and try to follow the path via ftp yourself. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011654 Share on other sites More sharing options...
stickynote427 Posted February 13, 2010 Author Share Posted February 13, 2010 Using require() unfortunately gives me the same result. Using file_get_contents() displays the rest of the page like I want it to, but the content of the example file is still not included. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011659 Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 You need to turn error reporting and display errors on. Put this at the very tp of your script. <?php ini_set('display_errors','1') ; error_reporting(E_ALL) ;?> Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011661 Share on other sites More sharing options...
stickynote427 Posted February 13, 2010 Author Share Posted February 13, 2010 Thanks. This is what I get: Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: NO) in /var/www/html/mysql/test.php on line 4 I have tried different usernames and passwords as well and those do not work, either. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011662 Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 You haven't posted any code that connects to any databases. That error would seem unrelated. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011665 Share on other sites More sharing options...
stickynote427 Posted February 13, 2010 Author Share Posted February 13, 2010 I'm confused. This is all of the code I have in my file: <?php ini_set('display_errors','1') ; error_reporting(E_ALL) ;?> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011667 Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 How does this relate to the code in your first post and then again to your question? Are you with us? Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011670 Share on other sites More sharing options...
cwarn23 Posted February 13, 2010 Share Posted February 13, 2010 Try making your connect line the following $con = mysql_connect("localhost","root","",true); Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011673 Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 Try making your connect line the following $con = mysql_connect("localhost","root","",true); Firstly, what does that have to do with the original question? Secondly, your advice would do NOTHING at all in relation to an access denied error either. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011675 Share on other sites More sharing options...
stickynote427 Posted February 13, 2010 Author Share Posted February 13, 2010 Oh, lord. I apologize. I'm also having problems with MySQL and did not realize that this was not where I was trying to get help with MySQL. I added the PHP code that thorpe provided earlier to the CORRECT file this time. Here is the error I receive: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /var/www/html/pages/howto/2/code2.txt on line 6 Again, I'm sorry about my mistake. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011677 Share on other sites More sharing options...
cwarn23 Posted February 13, 2010 Share Posted February 13, 2010 Php must be executing the text as code. Try the following as your include. echo file_get_contents('howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt'); Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011682 Share on other sites More sharing options...
stickynote427 Posted February 13, 2010 Author Share Posted February 13, 2010 Okay, now I receive: Warning: file_get_contents(howto/2/tut2.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /var/www/html/pages/howto.txt on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011684 Share on other sites More sharing options...
cwarn23 Posted February 13, 2010 Share Posted February 13, 2010 Isn't it obvious, the file does not exist. So I would suggest that you make sure the specified path is NOT a fake url and to make sure it exists. If you need a fake url you must specify the full 'http://domain.com/folderetc'. Okay, now I receive: Warning: file_get_contents(howto/2/tut2.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /var/www/html/pages/howto.txt on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011685 Share on other sites More sharing options...
greatstar00 Posted February 13, 2010 Share Posted February 13, 2010 the above error means the file does not exist, so try to give a valid file Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011687 Share on other sites More sharing options...
stickynote427 Posted February 13, 2010 Author Share Posted February 13, 2010 Alright, well, I tried this: echo file_get_contents('http://example.com/pages/howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt'); And I get this error message: Warning: file_get_contents(http://example.com/pages/howto/2/tut2.txt) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/html/pages/howto.txt on line 8 And went to http://example.com/pages/howto/2/tut2.txt and I can see the file fine. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011688 Share on other sites More sharing options...
cwarn23 Posted February 13, 2010 Share Posted February 13, 2010 Replace the example.com with your real domain. Alright, well, I tried this: echo file_get_contents('http://example.com/pages/howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt'); And I get this error message: Warning: file_get_contents(http://example.com/pages/howto/2/tut2.txt) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/html/pages/howto.txt on line 8 And went to http://example.com/pages/howto/2/tut2.txt and I can see the file fine. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011689 Share on other sites More sharing options...
stickynote427 Posted February 13, 2010 Author Share Posted February 13, 2010 http://blakechesney.com/pages/howto/2/tut2.txt Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011690 Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 This likely means that you have url_wrappers disabled. Most hosts will not allow you to use urls to include or open files. Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011691 Share on other sites More sharing options...
cwarn23 Posted February 13, 2010 Share Posted February 13, 2010 Try the following: echo file_get_contents('http://blakechesney.com/pages/howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt'); Quote Link to comment https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/#findComment-1011693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.