-2

I have written a code - It takes user input and displays sha1() string for the input.

I get the 'undefined index' notice on local host even after declaring the variables and using isset() function.

Notice: Undefined index: enc in C:\Program Files\EasyPHP-12.1\www\MySQL\localhost_drupal\encrypt.php on line 61

It works fine on the remote (production) server.

What is the fault in the following code?

<?php

//variable declaration

$enc = $len = "";

// checking with isset

if(isset($_POST['enc'])) {

//sha1()

$enc = sha1($_POST['enc']); 

// strlen function

$len = strlen($_POST['enc']);
}
?>

//HTML

<!DOCTYPE html>

<html>

<head><title>Generate Password</title></head>

<body>

<form name = "encry" action = "encrypt.php" method = "post">

<input type = "text" name = "enc" >

<input type = "submit" value = "Submit">

</form>

//OUTPUT

<?php




//Line 61 

echo "<p> You entered :" . $_POST['enc']."</p>";

echo "<p> Length of the string you entered :" . $len. "<p>";
echo "<p> Your Password is : ". $enc  . "</p>";

?>

</body>

</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) –  Dec 22 '14 at 03:34
  • 1
    Are you running this as one or two files? – Funk Forty Niner Dec 22 '14 at 03:37

2 Answers2

1

The reason being for the error is that your entire code is inside one page and errors out on initial load. Error reporting is set on one server, but isn't on the other.

You should be using isset() against a named submit button (which I have named) and around where you're using
echo "<p> You entered :" . $_POST['enc']."</p>"; etc..

Change your code to the following:

<?php

//variable declaration

$enc = $len = "";

// checking with isset

if(isset($_POST['enc'])) {

//sha1()

$enc = sha1($_POST['enc']); 

// strlen function

$len = strlen($_POST['enc']);
}
?>

//HTML

<!DOCTYPE html>

<html>

<head><title>Generate Password</title></head>

<body>

<form name = "encry" action = "encrypt.php" method = "post">

<input type = "text" name = "enc" >

<input type = "submit" value = "Submit" name = "submit">

</form>

//OUTPUT

<?php



if(isset($_POST['submit'])) {
//Line 61 

echo "<p> You entered :" . $_POST['enc']."</p>";

echo "<p> Length of the string you entered :" . $len. "<p>";
echo "<p> Your Password is : ". $enc  . "</p>";

}
?>

</body>

</html>

To set error reporting on:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

It's an E_NOTICE. Likely on production you suppress error messages (or at least notices). This is fine, but it does explain why it breaks in development.

Fix the issue and the notice will go away.

Halcyon
  • 57,230
  • 10
  • 89
  • 128