Is there a way avoid the appearance of notice msgs in PHP (in the executed page)? If yes, what's the method?

Dani AI

Generated

A few points to close the loop on the notice and make the login logic robust for years to come.

The immediate symptom ("Undefined variable: flag") happens because the code reads a variable that was never set. As showed, you can hide notices, and as pointed out, initializing a variable fixes the symptom. Those are quick fixes. A better solution is to redesign the login flow: validate inputs, query for the single matching user instead of scanning the whole table, use prepared statements to avoid SQL injection, store passwords hashed, and route with a lookup array instead of dozens of if/else headers. Also remember that PHP notices produce output which can break header() redirects.

Example modern pattern (PDO + prepared statements + password handling):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=db;charset=utf8mb4', 'root', '', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$username = filter_input(INPUT_POST, 'votername', FILTER_SANITIZE_STRING);
$password = $_POST['voterpwd'] ?? '';

if (!$username || $password === '') {
    echo 'You Are an Unauthorised User';
    exit;
}

$stmt = $pdo->prepare('SELECT vid, pwd, dst, voted FROM voter WHERE vid = :vid LIMIT 1');
$stmt->execute([':vid' => $username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if ($user && password_verify($password, $user['pwd'])) {
    if ($user['voted'] === '0') {
        $pdo->prepare('UPDATE voter SET voted = 1 WHERE vid = :vid')->execute([':vid' => $user['vid']]);
        $routes = ['Chittoor'=>'ctrvoters_poll.html','Goodoor'=>'gdrvoters_poll.html']; // add rest
        if (isset($routes[$user['dst']])) {
            header('Location: /project/Votingmodule/' . $routes[$user['dst']]);
            exit;
        }
        echo 'Polling page not found for your district';
    } else {
        echo 'Sorry! You can vote only once';
    }
} else {
    echo 'You Are an Unauthorised User';
}

Checklist and references:

  • Do not rely on hiding notices in production; fix root causes instead.
  • Migrate away from old mysql_* to PDO or MySQLi (PDO manual).
  • Use password_hash() / password_verify() for credentials (password_hash, password_verify).
  • Validate inputs with filter_input() (filter_input).
  • header() must run before any output (header).
  • Read OWASP guidance on SQL injection to avoid common pitfalls (OWASP SQL Injection).

This approach removes notices, improves security, and keeps redirects reliable.

Recommended Answers

All 6 Replies

When I give random values for login and password i.e. values which are not present in the database, the output I get is--
"Notice: Undefined variable: flag in C:\wamp\www\project\Loginmodule\loginconnect2.php on line 49
You Are an Unauthorised User."

How can I get rid of the error? The code is as follows.

<?php
//initilaize the mysql user name and password
//Database Config
$dbHost='localhost'; //Database server 
$dbName='db';  // Name of the database
$dbUser='root'; // Database username
$dbPass=''; // Database password
 
 
$source = mysql_connect($dbHost, $dbUser, $dbPass);
 
if (!$source) {
	die('Not connected : ' . mysql_error());
}
 
$db_selected = mysql_select_db('db', $source);
 
if (!$db_selected) {
	die ('Can\'t use $DB : ' . mysql_error());
}
 
//get the information from login form
 
$username = addslashes($_POST['votername']);
 
$pwd = addslashes($_POST['voterpwd']);

  //execute the query

  $sql ="SELECT * FROM voter";

  $result = mysql_query($sql);

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {

	  if($row['vid']==$username && $row['pwd']==$pwd)

	   {

	   	 $flag =1;

	   	 break;

	   }

    }

    if($flag == 1)
	{
     	 	if($row['voted']=='0')
     	 	{
     	 		$sql1="UPDATE voter SET voted='1' WHERE vid='".$row['vid']."'";
     	 		
     	 		$result1 = mysql_query($sql1);
     	 		if($row['dst']=='Chittoor')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ctrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Goodoor')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/gdrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Hyderabad')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/hydvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vijayawada')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vjwvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Nalanda')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/nldvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Patna')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ptnvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Rajgir')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/rjrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vaishali')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vsivoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Ahmedabad')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/amdvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Gandhinagar')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/gndvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Kaira')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/kravoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vadodara')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vdrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Ambala')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ambvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Jakal')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/jklvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Sohna')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/shnvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Panipat')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/pptvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Ernakulam')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ekmvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Kozhikode')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/kkdvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Thrissur')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/tsrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Trivandrum')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/tvmvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Chennai')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/chnvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Coimbatore')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/cbrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Thirunelveli')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/tnvvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vellore')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vlrvoters_poll.html");
     	 		}
     	 	}
 			else if($row['voted']=='1')
     	 	{
  				echo "Sorry!!!You can vote only once";   	 		
       	 	}
    }

    else

    {

      	  echo "You Are an Unauthorised User";

	  //how to call next \html page


    }


    mysql_free_result($result);

    mysql_close($source);

?>

It worked! Thank you so much! :)

as well as turning of reporting, why not fix the error

$flag is defined with a true value in a subroutine at line 34, if the subroutine is not executed there is nothing for line 49 to examine
before the subroutine in line 34, set $flag to false,
at line 33 $flag==0; so there is something for the test in line 49 to examine

<?php //initilaize the mysql user name and password
//Database Config
$dbHost='localhost'; //Database server 
$dbName='db'; // Name of the database
$dbUser='root'; // Database username
$dbPass=''; // Database password
$source = mysql_connect($dbHost, $dbUser, $dbPass);
if (!$source) { die('Not connected : ' . mysql_error()); }
$db_selected = mysql_select_db('db', $source);
if (!$db_selected) { die ('Can\'t use $DB : ' . mysql_error()); }
 //get the information from login form
$username = addslashes($_POST['votername']);
$pwd = addslashes($_POST['voterpwd']);
//execute the query
$sql ="SELECT * FROM voter";
$result = mysql_query($sql);
$flag = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if($row['vid']==$username && $row['pwd']==$pwd) {
$flag =1;
break; } }
if($flag == 1) {
if($row['voted']=='0') {
$sql1="UPDATE voter SET voted='1' WHERE vid='".$row['vid']."'";
$result1 = mysql_query($sql1);
if($row['dst']=='Chittoor') { header("Location:http://localhost/project/Votingmodule/ctrvoters_poll.html"); }
elseif($row['dst']=='Goodoor') { header("Location:http://localhost/project/Votingmodule/gdrvoters_poll.html"); }
elseif($row['dst']=='Hyderabad') { header("Location:http://localhost/project/Votingmodule/hydvoters_poll.html"); }
elseif($row['dst']=='Vijayawada') { header("Location:http://localhost/project/Votingmodule/vjwvoters_poll.html"); }
elseif($row['dst']=='Nalanda') { header("Location:http://localhost/project/Votingmodule/nldvoters_poll.html"); }
elseif($row['dst']=='Patna') { header("Location:http://localhost/project/Votingmodule/ptnvoters_poll.html"); }
elseif($row['dst']=='Rajgir') { header("Location:http://localhost/project/Votingmodule/rjrvoters_poll.html"); }
elseif($row['dst']=='Vaishali') { header("Location:http://localhost/project/Votingmodule/vsivoters_poll.html"); }
elseif($row['dst']=='Ahmedabad') { header("Location:http://localhost/project/Votingmodule/amdvoters_poll.html"); }
elseif($row['dst']=='Gandhinagar') { header("Location:http://localhost/project/Votingmodule/gndvoters_poll.html"); }
elseif($row['dst']=='Kaira') { header("Location:http://localhost/project/Votingmodule/kravoters_poll.html"); }
elseif($row['dst']=='Vadodara') { header("Location:http://localhost/project/Votingmodule/vdrvoters_poll.html"); }
elseif($row['dst']=='Ambala') { header("Location:http://localhost/project/Votingmodule/ambvoters_poll.html"); }
elseif($row['dst']=='Jakal') { header("Location:http://localhost/project/Votingmodule/jklvoters_poll.html"); }
elseif($row['dst']=='Sohna') { header("Location:http://localhost/project/Votingmodule/shnvoters_poll.html"); }
elseif($row['dst']=='Panipat') { header("Location:http://localhost/project/Votingmodule/pptvoters_poll.html"); }
elseif($row['dst']=='Ernakulam') { header("Location:http://localhost/project/Votingmodule/ekmvoters_poll.html"); }
elseif($row['dst']=='Kozhikode') { header("Location:http://localhost/project/Votingmodule/kkdvoters_poll.html"); }
elseif($row['dst']=='Thrissur') { header("Location:http://localhost/project/Votingmodule/tsrvoters_poll.html"); }
elseif($row['dst']=='Trivandrum') { header("Location:http://localhost/project/Votingmodule/tvmvoters_poll.html"); }
elseif($row['dst']=='Chennai') { header("Location:http://localhost/project/Votingmodule/chnvoters_poll.html"); }
elseif($row['dst']=='Coimbatore') { header("Location:http://localhost/project/Votingmodule/cbrvoters_poll.html"); }
elseif($row['dst']=='Thirunelveli') { header("Location:http://localhost/project/Votingmodule/tnvvoters_poll.html"); }
elseif($row['dst']=='Vellore') { header("Location:http://localhost/project/Votingmodule/vlrvoters_poll.html"); }
}
elseif($row['voted']=='1') { echo "Sorry!!!You can vote only once"; }
}
else { echo "You Are an Unauthorised User"; }
//how to call next \html page
mysql_free_result($result);
mysql_close($source);?>

Thank you! I made that addition. :)

oh yeah, elseif() if minutely faster than else if()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.