PHP Web Host - Quality Web Hosting For All PHP Applications Sign up for PayPal and start accepting credit card payments instantly
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
Darrell3831
Worker
Worker


Joined: Feb 18, 2004
Posts: 244

PostPosted: Wed Feb 18, 2004 3:31 pm Reply with quote Back to top

Hi,

I'm developing a php module for my nuke site and I'd like to ask a question about flow control.

At the end of many functions within nuke you see things like this:

Code:
header("Location: admin.php?op=addStudent&msg=$msg");


From my C backbround I can tell this is a type of branch. Your code continues execution with the supplied function and arguments.

However this is not absolute. It cought me off guard today when these lines were totally ignored and program execution continued to flow through my if statements.

Here's my sample:

Code:
function insertStudent($name, $nukeid, $cid, $status){
   global $prefix, $db;

   $name=filter_text($name, "nohtml");
   $nukeid=intval($nukeid);  // Make sure it's an intiger
   
   $username = isSiteMember($nukeid);
   
   if($username == NULL) {
      $msg = _STUDENT_NOTFOUND;  // Nuke id does not exist
      header("Location: admin.php?op=addStudent&msg=$msg");
   }
   
   $sid = isStudent($nukeid);
   if ($sid) {
      $msg = _STUDENT_ALREADY_IN; // Student is already in university - cant add again
      header("Location: admin.php?op=addStudent&msg=$msg");
   }

   if ($name!="") {
      $username=$name;
   }
   
   $insert = $db->sql_query("INSERT INTO ".$prefix."_university_students VALUES (NULL, '$nukeid', '$username', '$status')");
}



In this bit of code I first expected program execution to skip the sql insertion if certain conditions were met, however that is not the case.

Even when the if statement pulls you in you still don't branch immediately. The sql insert is still done, even though I did not want it. Sad

Do I have a syntax error, or is this not the way to control a programs flow?

Thanks,
Darrell
View user's profile Send private message Visit poster's website
Raven
Site Admin/Owner


Joined: Aug 27, 2002
Posts: 16987
Location: Kansas

PostPosted: Wed Feb 18, 2004 3:48 pm Reply with quote Back to top

Kinda-sorta. It actually writes an HTTP header and as such, no code can be written to the browser before it or you will receive the 'headers already sent' message unless output_buffering is on.

I'm not sure exactly where your code fails, but I would rewrite this statement
Code:
if($username == NULL) {
as
Code:
if (isnull($username)) {
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
Raven
Site Admin/Owner


Joined: Aug 27, 2002
Posts: 16987
Location: Kansas

PostPosted: Wed Feb 18, 2004 3:52 pm Reply with quote Back to top

Oh, and it is a good idea to add die(); after those header statements Smile
Code:
header("Location: admin.php?op=addStudent&msg=$msg");
die();
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
Darrell3831
Worker
Worker


Joined: Feb 18, 2004
Posts: 244

PostPosted: Wed Feb 18, 2004 4:01 pm Reply with quote Back to top

Thanks for the tip, I rewrote it to if (is_null($username)).

I've been trying to figure this out now for over 7 hours. Something isent right with this, or with my understanding of it.

The code isent exactly failing. I use the two if statements to return an error message to the addStudent function so that the user can give it antoher shot.

They are both actually working. I've put echo statements on every single line and watched my data as I step thru it. I'm droping into the if statements and upon exit of this function I'm going to the appropriate spot..

The problem is that the insert command runs no matter what I do.

If I'm error trapping I don't want the insert command to run on bad data. What am I missing?

I don't believe those header statements are right...
View user's profile Send private message Visit poster's website
Darrell3831
Worker
Worker


Joined: Feb 18, 2004
Posts: 244

PostPosted: Wed Feb 18, 2004 4:03 pm Reply with quote Back to top

OMG,

That fixed it!

Thanks... What a waste of a day...... geessss.
View user's profile Send private message Visit poster's website
Raven
Site Admin/Owner


Joined: Aug 27, 2002
Posts: 16987
Location: Kansas

PostPosted: Wed Feb 18, 2004 4:11 pm Reply with quote Back to top

I thought that litlle nuance might just do it Wink
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
Display posts from previous:       
Post new topic   Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2011 by Raven

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::
:: fisubice Theme Modified by the RavenNuke™ Team ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum