Jump to content

[SOLVED] Equal variables are.....not equal ?


dexter_sherban

Recommended Posts

I have a program that takes a value from a column and stores it into a variable. Then takes a value the user entered in a html form and stores it in another variable. Then it should see if they are the same(they are both strings). The site works, but I kept getting them unequal. And when I was tesing it I knew the valuse were equal. So I echoed them before the comparison and both are "aaaa". But the if statement sais they are different. The code is big so il try to but only the relevant lines.

 

<?php 

if (isset($_POST['actualusername']))
{
$actualusername=$_POST['actualusername'];
}
else
{
$actualusername=$_POST['actualusername'];
}
if (isset($_GET['username']))
{
$username=$_GET['username'];
}
else
{
$username=null;
}
if (isset($_POST['new_password1']))
{
$new_password1=$_POST['new_password1'];
}
else
{
$new_password1=null;
}
if (isset($_POST['new_password2']))
{
$new_password2=$_POST['new_password2'];
}
else
{
$new_password2=null;
}
if (isset($_POST['old_password']))
{
$old_password=$_POST['old_password'];
}
else
{
$old_password=null;
}
if (isset($_POST['initial']))
{
$initial=$_POST['initial'];
}
else
{
$initial=null;
}
$date=date('y-m-d');

$con=mysql_connect("localhost","root","j3000");
if (!$con)
{
die ('could not connect: ' .mysql_error());
}
mysql_select_db("partners",$con);

if (isset($actualusername))
{
$data=mysql_query("select * from partners where username='$actualusername'");
$row = mysql_fetch_array($data);

$access_level=$row['acces_level'];
$actualusername=$row['username'];
$password_change=$row['password_change'];
$actual_old_password=$row['password'];
}

if (isset($username))
{
$data=mysql_query("select * from partners where username='$username'");
$row = mysql_fetch_array($data);

$access_level=$row['acces_level'];
$actualusername=$row['username'];
$password_change=$row['password_change'];
$actual_old_password=$row['password'];
}
?>

.
.
.
<?php 
if ($password_change == 0)
	{
	echo "To activate your account you must change your password";
	echo "<br>";
	?>
	<form action='partnersPasswordChange.php' method='POST' />
	<table width="500" border="1">
          <tr>
            <td width="200"><label for="name">Old Password:</label></td>
            <td width="300"><input type='password' id="textinput" name='old_password' class="textinput"/></td>
          </tr>
          <tr>
            <td><label for="name">New Password:</label></td>
            <td width="300"><input type='password' id="textinput" name='new_password1' class="textinput"/></td>
          </tr>
          <tr>
            <td><label for="name">Reapeat New Password:</label></td>
            <td width="300"><input type='password' id="textinput" name='new_password2' class="textinput"/></td>
          </tr>
        </table>
	<input type='hidden' name='actualusername' value='<?php echo ($username);?>'>
	<input type='hidden' name='initial' value=1>
	<input type='submit' class="buttonSubmit" value="Submit"/><br><br>
	</form>
      	<?php 
	if ($initial==1)
		{
			echo $actual_old_password;
			echo " ";
			echo $old_password;
			$counter=0;
			if($new_password1=="" || $new_password2=="" || $old_password="")
			{
			echo "Please fill in every field";
			echo "<br>";
			$counter++;
			}
			if($new_password1 != $new_password2)
			{
			echo "The new password you enter does not match";
			echo "<br>";
			$counter++;
			}
			if($old_password != $actual_old_password)
			{
			echo "The old password does not match";
			echo "<br>";
			$counter++;
			}
			if(strlen($new_password1) < 4 || strlen($new_password2) < 4)
			{
			echo "Password Must be longer than 4 characters";
			echo "<br>";
			$counter++;
			}
			if ($counter==0)
	 		{
			$upload=mysql_query("update partners set password='new_password1', password_change=1, password_change_date='$date' where username='$actualusername'");
			}
		}
	}
else
	{
	echo "This account has already been validated";
	}
	?>

 

Sry its soo big, I though most of it is relevant :|

Link to comment
Share on other sites

sometimes i find that IF statements have to hold each rule in (). You have:

 

if($new_password1=="" || $new_password2=="" || $old_password="")

 

try going like this:

 

if((!isset($new_password1)) || (!isset($new_password1))  || (!isset($new_password1)) 

 

See how I included each rule in it's own set of ()'s ??  Also, I went ahead and cleaned up the code.  It normally isn't best to check a varialbe like you was, because it doesn't always catch like you want it to.  Instead you use isset($variable) to see if the variable is set.  If you want to see if the variable isn't set you go:  !isset($variable)  reading, litterally, if isn't set variable then do { }

 

Anyways, like mpharo said, we need the exact error with line number. i'm just stabbing in the dark, but that is one of the things I seen wrong with your code.

Link to comment
Share on other sites

Thank you for your answers. My problem was with this IF statement

 

if ( $old_password !=  $actual_old_password )
			{
				echo "The old password does not match";
				echo "<br>";
				$counter++;
			}

 

 

The REALY wierd thing is, ive tried setting both variables just before the if statement.

 

$actual_old_password="eeee";
$old_password="eeee";

 

So know I know it doesnt have to do with variable types. But I still get them as not equal. Tried casting both to string, tried almost everything...

Link to comment
Share on other sites

I think you need to use strcmp or stricmp

 

PHP is comparing the location of the string in memory, so if you did:

 

$a = 'a';

$b = $a;

 

 

Then I think ($a == $b) == true, but ($b == 'a') == false, because $b points to a location of a string that holds 'a' which is not the same location as the 'a' in the expression.

 

this is pretty obvious if you code in c or c++...

 

monk.e.boy

Link to comment
Share on other sites

Try

 

<?
if ( $old_password !=  $actual_old_password )
{
echo "The old password does not match";
echo "<br>";
$counter++;
echo "-Not same-<br>";
echo "OLD=[$old_password]<br>";
echo "actualOLD=[$actual_old_password]";
}else{
echo "-same-<br>";
echo "OLD=[$old_password]<br>";
echo "actualOLD=[$actual_old_password]";
}
die;
?>

Link to comment
Share on other sites

OMG. two days of figuring it out...

 

if($new_password1=="" || $new_password2=="" || $old_password="")

 

$old_password=""....one equal...well its solved

 

Heh. Why does PHP allow you to assign inside an IF statement. Madness. The parser should check this. Python checks and throws and error if you try this.

 

monk.e.boy

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.