younis Posted April 18, 2011 Share Posted April 18, 2011 Hi, This is fairly urgent, so I appreciate any help anybody can give me ASAP. I recently upgraded from PHP 4 to 5 and now certain "include" code is not working on my site. Here's an example: // if topic selected, display the relevant content. if ($topic == 'page1') { include ("$topic.php"); } elseif ($topic == 'page2') { include ("$topic.php"); } // else wrong or no link selected. else { print "Go to the <A HREF = 'main.php'>Main page</A>"; } Let's say the above code was in a PHP file called "frontpage.php". It used to be that if a person went to "mydomain.com/frontpage.php?topic=page1" then the page called for in the above code would include the content of "page1.php" in "frontpage.php". It's no longer working. Any ideas why or how to fix it? Any better way of doing this? Thanks, Steve Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted April 18, 2011 Share Posted April 18, 2011 The code is probably written to rely on register_globals being ON in the php.ini file (which it really shouldn't be). Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202797 Share on other sites More sharing options...
younis Posted April 18, 2011 Author Share Posted April 18, 2011 And how do I fix that? Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202799 Share on other sites More sharing options...
Pikachu2000 Posted April 18, 2011 Share Posted April 18, 2011 By assigning the variables the proper values from the $_GET/$_POST/$_COOKIE/$_SESSION arrays such as $topic = $_GET['topic'] in every script that uses values from one of the superglobal arrays. Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202805 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 To fix the code, you have to tell PHP where to find the value. Since it's coming from the URL, the value will be in the $_GET array: <?php // if topic selected, display the relevant content. if ($_GET['topic'] == 'page1') { include ("{$_GET['topic']}.php"); } elseif ($topic == 'page2') { include ("{$_GET['topic']}.php"); } // else wrong or no link selected. else { print "Go to the <A HREF = 'main.php'>Main page</A>"; } ?> But, I would re-write it as: <?php // if topic selected, display the relevant content. $valid_pages = array('page1','page2'); if (in_array($_GET['topic'],$valid_pages)) { include("{$_GET['topic']}.php"); } // else wrong or no link selected. else { print "Go to the <A HREF = 'main.php'>Main page</A>"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202806 Share on other sites More sharing options...
younis Posted April 18, 2011 Author Share Posted April 18, 2011 Thanks for the suggestion, but your code gives me the following error: Parse error: syntax error, unexpected T_STRING Am I missing something? Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202815 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 Which code gives you the error & on which line? My suggested code snippet is error free. Ken Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202820 Share on other sites More sharing options...
younis Posted April 18, 2011 Author Share Posted April 18, 2011 Line 3 in the version with the Array. Parse error: syntax error, unexpected T_STRING in /.../htdocs/comics/comics.php on line 3 http://www.supermanhomepage.com/comics/comics.php?topic=comics-coming Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202823 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 Please post your code. You may have mis-typed my code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202829 Share on other sites More sharing options...
younis Posted April 18, 2011 Author Share Posted April 18, 2011 It's okay. I've resolved the problem. I re-typed the code and it worked. Quote Link to comment https://forums.phpfreaks.com/topic/234010-include-code-not-working/#findComment-1202833 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.