Jump to content

[SOLVED] Why?


Trium918

Recommended Posts

Ok, when register_global = Off php does not

process variables from a form with out using

$_POST or $_GET etc, correct? Well, I have

two variables which I set using the $_POST

method, but when I use isset() I am not able

to view my content in the browser. Why?

 

Example:

<?
  // register_global = Off
  // inside the php.ini configuration file
  $username = addslashes($_POST['username']);
  $password = addslashes($_POST['password']);
  
  //if(!($username) && !($password)) Works

  if(!isset($username) && !isset($password)) // Doesn't work
  {
?>

<form action="" method="post">
<table width="200" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>Username:</td>
<td><input type="text" name="username"  /></td>
</tr>
<tr>
     <td>Password:</td>
     <td><input type="text" name="password" /></td>
     </tr>
     <tr>
        <td align="center" colspan="2"><input type="submit" value="Log in" /> </td>
     </tr>
   </table>
  </form>

<?
}
else
{
  // Connect to MySql
$db = mysql_connect("localhost") or die(mysql_error());

// Select the appropriate database
mysql_select_db("member_auth") or die(mysql_error());

// query the database to see if there is a record which matched
$query = "SELECT count(*) FROM user_info_auth WHERE
username ='$username' AND 
password ='$password' ";
		  
$result = mysql_query($query);
if (!$result)
echo " Cannot run query!!";

$count = mysql_result($result, 0, 0);
if($count > 0){
//visistor's name and password combination are correct
echo "Welcome ".$username;
}
  else{
    echo"Go Away!!";
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/45914-solved-why/
Share on other sites

do

 

print_r($_POST);

 

and see what your post is returning

 

Thanks but I just wanted to know why was it working with

isset().

 

// register_global = Off
  // inside the php.ini configuration file
  $username = addslashes($_POST['username']);
  $password = addslashes($_POST['password']);
  
  //if(!($username) && !($password)) Works

  if(!isset($username) && !isset($password)) // Doesn't work

Link to comment
https://forums.phpfreaks.com/topic/45914-solved-why/#findComment-223027
Share on other sites

do

 

print_r($_POST);

 

and see what your post is returning

 

Thanks but I just wanted to know why was it working with

isset().

 

// register_global = Off
  // inside the php.ini configuration file
  $username = addslashes($_POST['username']);
  $password = addslashes($_POST['password']);
  
  //if(!($username) && !($password)) Works

  if(!isset($username) && !isset($password)) // Doesn't work

 

isset will be always true if the variable is set and since the global array contains the index $_POST['username'] you will always get true even if username was blank.

 

 

Link to comment
https://forums.phpfreaks.com/topic/45914-solved-why/#findComment-223029
Share on other sites

You want to use stripslashes() not addslashes() here:

<?php
  $username = addslashes($_POST['username']);
  $password = addslashes($_POST['password']);
?>

 

You should be testing whether the value exists in the $_POST array before you set your variables:

<?php
// register_global = Off
  // inside the php.ini configuration file
  if(!isset($_POST['username']) && !isset($_POST['password']))
  { ?>
<form action="" method="post">
<table width="200" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>Username:</td>
<td><input type="text" name="username"  /></td>
</tr>
<tr>
     <td>Password:</td>
     <td><input type="passord" name="password" /></td>
     </tr>
     <tr>
        <td align="center" colspan="2"><input type="submit" value="Log in" /> </td>
     </tr>
   </table>
  </form>
<?php
}
else
{
  $username = stripslashes($_POST['username']);
  $password = stripslashes($_POST['password']);
  // Connect to MySql
$db = mysql_connect("localhost") or die(mysql_error());
// Select the appropriate database
mysql_select_db("member_auth") or die(mysql_error());
// query the database to see if there is a record which matched
$query = "SELECT count(*) as cnt FROM user_info_auth WHERE username ='$username' AND  password ='$password' ";
$result = mysql_query($query) or die("Can not run query <pre>$query</pre><br>" . mysql_error());
$rw = mysql_fetch_assoc($result);
if($rw['cnt'] > 0)
    echo "Welcome ".$username; //visistor's name and password combination are correct
else
    echo"Go Away!!";
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/45914-solved-why/#findComment-223032
Share on other sites

It was working with out the isset(), but I wanted it the

way Ken set it up for me. Thanks.

 

One more question.

Ok, the page is submitting back to self, but

I once read on a post that leaving the action

attribute blank isn't a good idea. Why and would

I use [Location: login.php] in side the action

attribute?

Link to comment
https://forums.phpfreaks.com/topic/45914-solved-why/#findComment-223045
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.