Having two undefined indices (email and code, probably because of my htaccess file)

I am making a mailing system where the user enters their mail in their input text and clicks the "Submit" button to subscribe to the newsletter.

After he clicks the "Submit" button, he will receive an email stating that in order to confirm his subscription, he needs to click the link that I provide in this email.

This is a link:

<p>To confirm your subscription click on link below:</p>
    <a href="http://localhost/website/newsletter/confirm?email='.$email.'&amp;code='.$code.'">Confirm Subscription</a>

      

And this link will be redirected to my news confirmation page, where I will get the email and code and then make an update to my subscribers table.

$email = $_GET['email'];
$code= $_GET['code'];
$pdo = start();
$updSub = $pdo->prepare("UPDATE subscribers set status= ? WHERE code = ?");
$updSub->bindParam(2,$code);
$updSub->execute();

      

But I have these two notifications:

Note: Undefined index: email at F: \ Xampp \ htdocs \ website \ newsletter \ confirm.php

Note: Undefined index: code in F: \ Xampp \ htdocs \ website \ newsletter \ confirm.php

Do you understand why this can happen? I am using .htaccess file, dont know if it might be because of this, some problem with passing code and email variables in url.

This is my htaccess file:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

      

My query string:

@$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);

if(file_exists('template/'.$url[0].'.php')){
    require_once('template/'.$url[0].'.php');
}elseif(@file_exists('template/'.$url[0].'/'.$url[1].'.php')){
    require_once('template/'.$url[0].'/'.$url[1].'.php');
}
else{
    require_once('template/404.php');
}

      

+3


source to share


1 answer


First, when you handle user login and put it directly in the url, make sure the url is encoded. Even if email addresses cannot have spaces, etc.

If you are using JavaScript for processing use:

encodeURIComponent("");
encodeURIComponent(document.getElementById('email').value);
encodeURIComponent($("#email").val());  // jQuery Library Required

      

If you are using a form to handle user input without javascript intervention, don't worry about url encoding, this should happen automatically.

If PHP is used to handle input -> url then use:

rawurlencode($email); 

      

After that undefined index

happens when you call a variable without first declaring it. It's like saying:



echo $apples;  // where $apples has not been previously declared

      

Now check any files include

or require

to make sure you are not calling the name of the same variable.

Using functions like isset

and empty

will give you a better test result.

if(!empty($_GET['email'])){
     mysqli->real_escape_string($email = $_GET['email']);
}else{
     die("No E-Mail");
}

      

And will stop undefined index errors, IT IS PROVIDED to properly check to make sure the variables exist.

Plain and simple, the problem is that you are querying the variables before they are processed. This problem would not occur if their value was NULL.

0


source







All Articles