Jump to content

Problem with Global Variables


davil

Recommended Posts

        $errors=0;

         //NEW USER NAME HAS BEEN SUBMITTED, CHECK IT OUT AND THEN ADD IF OKAY
$newuser=$_REQUEST['newuser'];

function TryAgain(){
global $errors;
$errors++;
echo '<br/><br/>Please try again <a href="index.php?locate=admin&sub=add_admin">here</a>';
echo "<br/>errors=[$errors]";
}



echo "<br/>Trying to add user: $newuser<br/><br/>";
if ($newuser){
}else{
// field was left blank, report this to user and give them option to try again
echo '<br/>Field was left empty, you must pick a username.';TryAgain();
}

if (strlen($newuser)<5){echo 'Username should be at least 5 characters.';TryAgain();}

if (!$errors){echo "continue to SQL [errors=$errors]";}

 

 

what is wrong with this code?

For some reason if I increment $errors with $errors++  within the function it doesn't increment outside the function's scope. even though I declare it as a global at the start of the function. I know I'm doing something stupid here but can somebody please tell me what it is?

 

Thanks

Link to comment
Share on other sites

ah I have noticed that the way global variables worked in PHP4 has changed kinda since PHP5, as a security measure - is this right?

 

Here's some info

http://wiki.lunarpages.com/PHP_and_Register_Global_Variables

 

unfortunately that applies to POST and GET stuff and I'm just wondering about within a function? should it not still work?

I'm hesitant to change the setting in PHP.ini in case this is a seperate thing altogether.

 

 

Link to comment
Share on other sites

Register globals was a mechanism that you could turn on that would make all input variables global.  That can be a security hole in poorly written applications, so it is deprecated, but regardless, it's an option that you enable in the php.ini and has nothing to do with declaring a variable global inside a function.  That works, so if you're having an issue it probably is a logic error in your code, as you surmised it to be.

Link to comment
Share on other sites

but not within my program in an Else{ } block

 

If you directly want help with what your actual code is doing, it would be quickest if you just posted all the relevant code and stated exactly what symptom you are getting that makes you think it is not working (perhaps you are overwriting the variable somewhere?)

 

You are posting a few lines of your code taken out of context and asking someone who is not standing right next to you to tell you why your program is not doing what you expect.

Link to comment
Share on other sites

Yes that's very true. here's the code in its entirety (I've moved the function to the very top of the code now outside the else statement but it's still not working)

 

 

<?php$errors=0;  // set at 0 at the very start {doesn't seem to help though}function TryAgain(){global $errors; //reference the global variable instead of just internal$errors++; //increment the $errors variable;echo '<br/><br/>Please try again <a href="index.php?locate=admin&sub=add_admin">here</a>';echo "<br/>errors=[$errors]";}?><h1>Add a new Administrator Account</h1><?phpif (!isset($_REQUEST['frubmit'])){//check who's logged in$currentuser=$_SESSION["login"];ECHO <<<HEREZ<br/>Here is a list of the current users:<br/><table class='userlist2'><tr>HEREZ;$sql="SELECT `login` FROM `users`";$result = mysql_query($sql) or die (mysql_error());while ($row = mysql_fetch_array($result)){foreach ($row as $key => $value){$$key = stripslashes($value);}echo "<td>$login</td>";}echo <<<HEREZ</tr></table><form enctype="multipart/form-data" method="post" action="index.php"><input type="hidden" name="locate" value="admin"><input type="hidden" name="sub" value="add_admin"><br/>Add a new username:<input type='text' name='newuser' /><input type='submit' name='frubmit' /></form>HEREZ;}else{//NEW USER NAME HAS BEEN SUBMITTED, CHECK IT OUT AND THEN ADD IF OKAY$newuser=$_REQUEST['newuser'];echo "<br/>Trying to add user: $newuser<br/><br/>";if ($newuser){}else{// field was left blank, report this to user and give them option to try againecho '<br/>Field was left empty, you must pick a username.';TryAgain();}if (strlen($newuser)<5){echo 'Username should be at least 5 characters.';TryAgain();}if (!$errors){echo "continue to SQL for adding a user [errors=$errors]";}}?>

 

 

Basically I don't want it to continue to the SQL stuff (not written yet) unless $errors evaluates to false (or 0).

I increment $errors in the TryAgain() function, but for some reason in the above code it doesn't stick.

 

The way I check this is I'm echoing out the value of $errors within the function and outside the function.

If you set $newuser to something like 'abc' it echoes out that it is too short, and echoes out $errors as 1 , but then outside the function it reverts back to 0

Link to comment
Share on other sites

This is strange, seems it works fine as standalone PHP but when included into my project at large, it doesn't work as expected. I've checked and there are no other references to $errors, I even changed to $errorz, still no luck...

 

I'll bet there's an easy fix for this but I'm lost for the moment, will keep trying..

Link to comment
Share on other sites

eff it, for now I just replaced it with

 

$errors++;TryAgain();

 

and removed the increment from the function. it makes no sense why it wouldn't work, but the site isn't mission critical so I don't need to spend too much time on it.

 

Thanks anyway for all your help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.