0

I want to make a account activation page in php. I tried with these code, but every time it show

Your verification link is invalid or expired

whenever the email id and secret key is in my sql database what's wrong with my code? please help

<?php 
    $hash = $_GET['hash'];
    $e = base64_decode($_GET['e']);
?>
<?php require_once("Connections.php");?>
<?php 
    mysql_select_db($database, $connection);
    $sql=mysql_query("SELECT FROM temp  WHERE email='$e' AND hashkey='$hash'");

    if(mysql_num_rows($sql)!=1) {
        echo "Your verification link is invalid or expired";
    }
    else {
        if ($sql=mysql_query("DELETE FROM temp  WHERE email='".$e."' AND hashkey='".$hash."'")) {
            echo '<script type="text/javascript">
                  register_2($e);
                  function register_2(){
                      alert("Hi your email"+m);
                  }
                  </script>
                 ';
        }
    }    
?>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
SUJOY ROY
  • 29
  • 1
  • 1
  • 3

3 Answers3

0
<?php 
$hash = $_GET['hash'];

$e = base64_decode($_GET['e']);

?>


<?php require_once("Connections.php");?>
<?php 
mysql_select_db($database, $connection);
$sql=mysql_query("SELECT FROM temp  WHERE email='$e' AND hashkey='$hash'");
 if(mysql_num_rows($sql)<= 0)
   {
    echo "Your verification link is invalid or expired";
   }
else{
if ($sql=mysql_query("DELETE FROM temp  WHERE email='".$e."' AND hashkey='".$hash."'")){
        echo '<script type="text/javascript">
                    register_2($e);
                    function register_2(){
                        alert("Hi your email"+m);
                        }
              </script>
              ';
        }
    }

?>

just change !=1 to <=0
I do it

SUJOY ROY
  • 29
  • 1
  • 1
  • 3
0

Firstly don't use mysql_* functions they are depracated and are removed in php 7, use mysqli_* or pdo instead

so change

if(mysql_num_rows($sql)!=1)

to

$rowsCount = mysql_num_rows($sql);

if ($rowsCount === 0 || $rowsCount === false) {
    die "Your verification link is invalid or expired";
}

also your code is vulnerable to SQL INJECTION you should escape hash and also XSS register_2($e); here I can pass anything encoded in base64 via URL.

$hash = mysql_real_escape_string($_GET['hash']);

also this code

mysql_query("DELETE FROM temp  WHERE email='".$e."' AND hashkey='".$hash."'")

can be changed to

mysql_query("DELETE FROM temp  WHERE email='$e' AND hashkey='$hash'")

it's the same, but in the best case you should use prepared statements.

Other suggestions:

  1. Change echo to die;
  2. Remove else condition when you do 1.
  3. Your else condition does not check if query really removed rows it only check if query was executed, you should use mysql_affected_rows() to check if rows were deleted or remove this if condition.
Robert
  • 19,800
  • 5
  • 55
  • 85
-1

You should use the condition like this.

if(mysql_num_rows($sql) != NULL) { echo "Your verification link is invalid or expired"; }

StackUser
  • 5,370
  • 2
  • 24
  • 44
Hassan Shahbaz
  • 596
  • 1
  • 14
  • 38