Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2012, 08:38 PM   PM User | #1
vishalonne
New Coder

 
Join Date: Nov 2009
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
vishalonne is an unknown quantity at this point
Value is POSTED definitly but isset() says NO....

Hi All
See the code give given below. I was fighting with this code since last 5 hours to know why isset() is eveluating the condition as false if value is posted exactly what it shall POST.
If I uncomment the line no. - 4,5,6,7,8 and put rest of the code from line no. 10 to 28 I can see the POSTED value .
Can Anyone help in this by any guidance or suggestion. I will be thankful.

PHP Code:
<?php
    
include 'dbconnection.php';
    include 
'functions.php';
    
//sec_session_start();
     //  $email = $_POST['logemail'];
     //  $password = $_POST['p'];
    //    echo $password;
    //    echo $email;
     // Our custom secure way of starting a php session. 
    
    
if(isset($_POST['logemail'], $_POST['p'])) { 
       
$email $_POST['logemail'];
       
$password $_POST['p']; // The hashed password.
       
if(login($email$password$mysqli) === true) {
          
// Login success
          //$url = 'mwq';
        //echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';  
        
echo $password;
        echo 
$email;
    
       } else {
          
// Login failed
          
header('Location: login.php?error=1');
       }
    } else { 
       
// The correct POST variables were not sent to this page.
       
echo 'Invalid Request Data Not POSTED';
    }
    
?>
vishalonne is offline   Reply With Quote
Old 10-02-2012, 08:44 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
You need to do this and see what your $_POST data contains:

print_r($_POST);

Thats the only way to see what is actually being sent to your script.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-02-2012, 09:01 PM   PM User | #3
vishalonne
New Coder

 
Join Date: Nov 2009
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
vishalonne is an unknown quantity at this point
Hello Tangoforce
Thank you for consideration
I used this just before isset() to test what $_POST is doing and output is -

array(0) { }

But interesting part of the code is if I remove the comment from these
// $email = $_POST['logemail'];
// $password = $_POST['p'];
// echo $password;
// echo $email;

and rest of the code I commented then I get waht I expect from POST.
vishalonne is offline   Reply With Quote
Old 10-02-2012, 09:08 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Ok, print_r($_REQUEST) please.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-02-2012, 09:38 PM   PM User | #5
vishalonne
New Coder

 
Join Date: Nov 2009
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
vishalonne is an unknown quantity at this point
Tested and output is - Array ( [error] => 1 )
vishalonne is offline   Reply With Quote
Old 10-02-2012, 09:55 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is this page login.php? The only code we can see here indicates that you have issued a header redirect and passed it an querystring of error=1 which happens to be the same as your request. If this is the case, it indicates that the POST it passed upon an initial login attempt, but the login() function itself is not returning boolean true.
Comment out that header() and try a var_dump($_POST); again.
Fou-Lu is offline   Reply With Quote
Old 10-02-2012, 10:04 PM   PM User | #7
vishalonne
New Coder

 
Join Date: Nov 2009
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
vishalonne is an unknown quantity at this point
Here is my index.php code -
Code:
<td>
<FORM ID="Login" ACTION="login.php" METHOD="POST">
<h1>welcome to the login page</h1>
please input the login details to create an account here<br />
<table border="2">
<tr>
<td>email :</td><td><input id="logemail" name="logemail" type="text" size"30"></input></td>
</tr>
<tr>
<td>password :</td><td><input id="logpass1" name="logpass1" type="password" size"20"></input></td>
</tr>
</table>
<input type="button" value="Login" onClick="formhash2(this.form,this.form.logpass1);">
</FORM>

<FORM ID="Register" ACTION="register.php" METHOD="POST">
<h1>welcome to the registration page</h1>
please input the registration details to create an account here<br />
<table border="2">
<tr>
<td>email :</td><td><input name="regemail" type="text" size"30"></input></td>
</tr>
<tr>
<td>password :</td><td><input id="regpass1" name="regpass1" type="password" size"20"></input></td>
</tr>
</table>
<input type="button" value="Register" onClick="formhash1(this.form,this.form.regpass1);">
</FORM>
</td>
This is the code of formhash2() and formhash1() -
Code:
// JavaScript Document csnip
function formhash2(form,password) {
	 // Create a new element input, this will be out hashed password field.
   alert(form.id + " " + password.value);
   var p = document.createElement("input");
       // Add the new element to our form.
   
   p.name = "p";
   p.type = "hidden"
   p.value = hex_sha512(password.value);
   // Make sure the plaintext password doesn't get sent.
   password.value = "";
   // Finally submit the form.
   form.appendChild(p);
   form.submit();
}

function formhash1(form,password) {
	alert(form.id + " " + password.value);
  // Create a new element input, this will be out hashed password field.
  var pl = document.createElement("input");
  // Add the new element to our form.
   
   pl.name = "pl";
   pl.type = "hidden"
   pl.value = hex_sha512(password.value);
   // Make sure the plaintext password doesn't get sent.
   password.value = "";
   // Finally submit the form.
   form.appendChild(pl);
   form.submit();

}
and finally this is the code for login() -
PHP Code:
function login($email$password$mysqli) {
   
// Using prepared Statements means that SQL injection is not possible. 
   
if ($stmt $mysqli->prepare("SELECT id, email, password, salt FROM members WHERE email = ? LIMIT 1")) { 
      
$stmt->bind_param('s'$email); // Bind "$email" to parameter.
      
$stmt->execute(); // Execute the prepared query.
      
$stmt->store_result();
      
$stmt->bind_result($user_id$username$db_password$salt); // get variables from result.
      
printf("%s %s\n"$username$db_password);
      
$stmt->fetch();
      
$password hash('sha512'$password.$salt); // hash the password with the unique salt.
 
      
if($stmt->num_rows == 1) { // If the user exists
         // We check if the account is locked from too many login attempts
         
if(checkbrute($user_id$mysqli) == true) { 
            
// Account is locked
            // Send an email to user saying their account is locked
            
return false;
         } else {
         if(
$db_password == $password) { // Check if the password in the database matches the password the user submitted. 
            // Password is correct!
 
               
$ip_address $_SERVER['REMOTE_ADDR']; // Get the IP address of the user. 
               
$user_browser $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.
 
               
$user_id preg_replace("/[^0-9]+/"""$user_id); // XSS protection as we might print this value
               
$_SESSION['user_id'] = $user_id
               
$username preg_replace("/[^a-zA-Z0-9_\-]+/"""$username); // XSS protection as we might print this value
               
$_SESSION['username'] = $username;
               
$_SESSION['login_string'] = hash('sha512'$password.$ip_address.$user_browser);
               
// Login successful.
               
return true;    
         } else {
            
// Password is not correct
            // We record this attempt in the database
            
$now time();
            
$mysqli->query("INSERT INTO login_attempts (user_id, time) VALUES ('$user_id', '$now')");
            return 
false;
         }
      }
      } else {
         
// No user exists. 
         
return false;
      }
   }

which is functions.php and this file include in login.php file which is mentioned in previous post
vishalonne is offline   Reply With Quote
Old 10-02-2012, 10:08 PM   PM User | #8
neclord02
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
neclord02 is an unknown quantity at this point
If you delete the comments it works? Try it like this see if it works:



Code:
<?php 
    include 'dbconnection.php'; 
    include 'functions.php'; 
    /* sec_session_start(); 
      $email = $_POST['logemail']; 
       $password = $_POST['p']; 
        echo $password; 
        echo $email; 
     Our custom secure way of starting a php session. */ 
     
    if(isset($_POST['logemail'], $_POST['p'])) {  
       $email = $_POST['logemail']; 
       $password = $_POST['p']; // The hashed password. 
       if(login($email, $password, $mysqli) === true) { 
          // Login success 
          //$url = 'mwq'; 
        //echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';   
        echo $password; 
        echo $email; 
     
       } else { 
          // Login failed 
          header('Location: login.php?error=1'); 
       } 
    } else {  
       // The correct POST variables were not sent to this page. 
       echo 'Invalid Request Data Not POSTED'; 
    } 
    ?>
neclord02 is offline   Reply With Quote
Old 10-02-2012, 10:13 PM   PM User | #9
vishalonne
New Coder

 
Join Date: Nov 2009
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
vishalonne is an unknown quantity at this point
No I mean to say if I only keep this part in login.php then why the value of _POST is displaying correctly
Quote:
sec_session_start();
$email = $_POST['logemail'];
$password = $_POST['p'];
echo $password;
echo $email;
Then why isset() is not getting the posted value on page?
vishalonne is offline   Reply With Quote
Old 10-02-2012, 10:15 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The initial page is login.php then.

Remove the header() call from there and at the top enable your error reporting:
PHP Code:
ini_set('display_errors'1);
error_reporting(E_ALL E_STRICT); 
If that runs with no errors, and the var_dump($_POST) produces the results as expected, then you need to debug this login() function.

Edit:
To answer your question just above here, it looks to me that your login() function hasn't been verified as working before this point. Since you force a redirection header you have no way to evaluate the $_POST as it has now become $_GET.
Fou-Lu is offline   Reply With Quote
Old 10-02-2012, 10:19 PM   PM User | #11
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
If you're using firefox, try a plugin called HttpFox. This will allow you to see the request and response headers. More importantly it will allow you to see what post data is being sent (if any).
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-02-2012, 11:39 PM   PM User | #12
vishalonne
New Coder

 
Join Date: Nov 2009
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
vishalonne is an unknown quantity at this point
@TangoForce
If I delete the entire code from isset to the bottom and leave only this part in login.php file -
PHP Code:
<?php
include 'dbconnection.php';
include 
'functions.php';

sec_session_start();
echo 
var_dump($_POST);
print_r($_REQUEST);

?>
See the ouptout -
Quote:
array(3) { ["logemail"]=> string(6) "ankush" ["logpass1"]=> string(0) "" ["p"]=> string(128) "704d3e76a26e1c6e99e8ca31237eb400cf2cb38b9712f22ee49ec4831bd974a37ef68fd3a8ee265b9a90cb2c07006c114db 59fccd93cc0a36458f9d3f04773ea" } Array ( [logemail] => ankush [logpass1] => [p] => 704d3e76a26e1c6e99e8ca31237eb400cf2cb38b9712f22ee49ec4831bd974a37ef68fd3a8ee265b9a90cb2c07006c114db5 9fccd93cc0a36458f9d3f04773ea )
vishalonne is offline   Reply With Quote
Old 10-03-2012, 12:28 AM   PM User | #13
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,516
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
So that only confirms then that your call to header() (as Fou keeps saying) is being called and your script is redirecting the browser back to login.php with a fresh http request. At that point, all of your $_POST data is lost. THAT is your problem.

This is how your logic is running:

Submit form to login.php
login.php runs (with $_POST data) isset code, calls header('Location: login.php?error=1')
Browser is redirected to login.php?error=1 - No $_POST data.

$_POST is only valid for the page it is sent to. By issuing a redirect, your browser is making a completely fresh http request. Because its a request via url and not a form submission, $_POST is gone / deleted / blackholed. This is because you issued a header() and THAT instance of the script was terminated.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:58 AM.


Advertisement
Log in to turn off these ads.