Variable Strangeness In Php Code

liunx

Guest
I built a basic content management system in php using mysql. I have it up and running fine on a local test server, using php 4.3.3 and the current build of mysql. When I tried to post it to my TCH account, I'm getting some odd results.<br /><br />Here is the first section of the code:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo("The request var for page is {$_REQUEST['page']}<br>");    //debugging code<br /><br />// Checks to see whether the requested page exists and sets a default page if it doesn't.<br />if ( isset($_REQUEST['page']) )<br />{<br />    $page->name = $_REQUEST['page'];<br />  echo("Requested page is $page->name");    // debugging code<br />    if (!entry_exists('pagedata','name',$page->name))<br />    {<br />  $page->name = "404";    // File Not Found Error Page<br />    }<br />}<br />else<br />{<br />    $page->name = "home";<br />}<!--c2--></div><!--ec2--><br /><br />The _REQUEST var is set properly. But $page->name (as well as any other variables that use object syntax) is null, which naturally breaks the rest of the code.<br /><br />The thing that makes this perplexing is that I have a completely different script that opens the <b>exact</b> same way, and it appears to be functioning properly. I even wrote a basic test script that just assigns and echoes $page->name and it works fine. All the code runs happily on my test server.<br /><br />I've tried re-ftp'ing the files to the host numerous times. I even tried to upload the content to another host as a test with the same results. So I'm pretty stumped as to what the problem could be, and why it works in one script but won't in another.<br /><br />Any ideas would be greatly appreciated. Thanks!<br /><br />Cheers,<br /><br />Chris<!--content-->
I'm not much of an OOP code writer, but shouldn't you have code like<br /><br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->$page = new Whatever;<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />That's the best I've got. Hope it helps.<!--content-->
Well, I'd say the same as Jack: have you created an instance of an object (which contains a $name member variable) named $page?<br /><br />Perhaps you forgot to include the file that defines the $page class?<br /><br />Another suggestion I'd make is to use $_GET instead of $_REQUEST. I see lots of people using $_REQUEST but I really don't understand why.<br />$_REQUEST has $_GET, $_POST and $_COOKIES all mixed in the same array, which, in my humble opinion, can be quite a mess.<br />I prefer to diferentiate them by using the different variables for each of them.<!--content-->
Thanks for the responses guys. I appreciate the feedback.<br /><br />I had wondered whether objects and array variables could be defined on the fly. It's my understanding that they can, based on other php sources I've seen. $page isn't necessarily a real object - it's more like an array that's retyped using object syntax.<br /><br />Part of the problem with defining $page ahead of time is its dynamic nature. My code is set up to use an html template whose sections are stored in a table. Content for each section is kept in a different table and is loaded into a variable based on the section name. Content is loaded into the section variable in the manner of<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$page->{$section->name} = <entry information>;<!--c2--></div><!--ec2--><br /><br />Because the section element of $page is created on the fly I don't see a simple way of declaring it beforehand. The code works well on the test server, but it broke once I uploaded it here.<!--content-->
I'm not positive, but maybe there is a name conflict between the $_REQUEST 'page' and the $page you create (since I think register globals is on here)? When you try to set $page->name making it an object it wont work since $page is first set as a normal varible as its sent to the script.<br /><br />Try renaming one of them once (for example change it to $mypage->name).<!--content-->
Yeah, what Frylock said was the first thing to come to mind when i saw your code as a possibility. The only other thing I could think of was to check the scope of your variables, but they're all fine from the looks of things.<br /><br />Good luck. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Renaming $page did the trick. So apparently there was indeed a conflict there. That also explains why the other script worked, since I use different _GET values in that one (I also took borfast's advice and changed _REQUEST to _GET - thanks). I also had to rename a couple other variables that had the same problem. But it's working great now. <br /><br />Woohoo! My first content management system!<br /><br />Thanks a lot for all the help you guys. It's greatly appreciated.<br /><br />If you want to see the first pass result, go to <!-- w --><a class="postlink" href="http://www.soulfulhealing.com">www.soulfulhealing.com</a><!-- w -->. After I've tweaked it a bit more I'll post it for critique.<br /><br />Regards,<br /><br />Chris<!--content-->
I really like the layout and the colors you're using.<!--content-->
Thanks for the compliment Jack. Thanks also for posting the "Ultimate Form Mail". It's saved me a lot of time coding the backend.<!--content-->
 
Top