davil Posted September 28, 2010 Share Posted September 28, 2010 $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 Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/ Share on other sites More sharing options...
davil Posted September 28, 2010 Author Share Posted September 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116694 Share on other sites More sharing options...
davil Posted September 28, 2010 Author Share Posted September 28, 2010 also the code I posted above seems to work fine on its own but not within my program in an Else{ } block, this must be the key, I'll probably figure it out now any second. Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116696 Share on other sites More sharing options...
gizmola Posted September 28, 2010 Share Posted September 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116702 Share on other sites More sharing options...
PFMaBiSmAd Posted September 28, 2010 Share Posted September 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116726 Share on other sites More sharing options...
davil Posted September 28, 2010 Author Share Posted September 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116734 Share on other sites More sharing options...
kickstart Posted September 28, 2010 Share Posted September 28, 2010 Hi Just tried your code and it works fine for me. The function increments $errors and when I echo it out afterwards it is still 1. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116737 Share on other sites More sharing options...
davil Posted September 28, 2010 Author Share Posted September 28, 2010 Thanks Keith. perhaps it is my server software (uniform server) - I will look into it. What version PHP and Apache you using ? if you're using Apache at all that is? Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116743 Share on other sites More sharing options...
davil Posted September 28, 2010 Author Share Posted September 28, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116744 Share on other sites More sharing options...
kickstart Posted September 28, 2010 Share Posted September 28, 2010 Hi No idea I am afraid. I am using a WAMP setup on my machine with php 5.3 and Apache 2.2.11 All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116748 Share on other sites More sharing options...
davil Posted September 28, 2010 Author Share Posted September 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214604-problem-with-global-variables/#findComment-1116756 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.