PHP Notice: Undefined index:

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
darklord2014

Mon Oct 24, 2016 12:26 am

I'm currently trying learn php and have been following a tutorial on YouTube that the code source was made availble to download as a reference aid for a php mysql site.
The problem that I have ran in to is that the source code was done for php5 but I have php7 and as a result there are changes that have been made that causes errors. I am currently trying to debug as I am learning but have run in to.....

/var/log/apache2/error.log

Code: Select all

[Mon Oct 24 01:52:02.769618 2016] [:error] [pid 2688] [client 127.0.0.1:58058] PHP Notice:  Undefined index: admin in /var/www/html/administrator/admin_check.php on line 27, referer: http://localhost/
admin_check.php (Note: this is the original source code that I am working with)

Code: Select all

<?php
/* Created by Adam Khoury @ www.developphp.com */

$error_msg = "";
if ($_POST['username']) {

	$username = $_POST['username'];
	$password = $_POST['password'];
	// Simple hard coded values for the correct username and password
	$admin = "theterminator";
    $adminpass = "goobernuts";
    // connect to mysql here if you store admin username and password in your database
    // This would be the prefered method of storing the values instead of hard coding them here into the script
    if (($username != $admin) || ($password != $adminpass)) {
		$error_msg = ': <font color="#FF0000">Your login information is incorrect</font>';
	} else {
        $_SESSION['admin'] != $username;
		require_once "index.php";
		exit();
	}

}// close if post username
?>


<?php
if($_SESSION['admin'] != "theterminator") {
    echo '<h3>Only the administrator can view this directory</h3><br />
	
	<table width="340" border="0">
<form action="admin_check.php" method="post" target="_self">
  <tr>
    <td colspan="2">Please Log In Here' . $error_msg . '</td>
  </tr>
  <tr>
    <td width="96">Username:</td>
    <td width="234"><input type="text" name="username" id="username" style="width:98%" /></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input type="password" name="password" id="password" style="width:98%" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" name="button" id="button" value="Log In Now" /></td>
  </tr>
</form> 
</table>
	<br />
<br />
<br />

<a href="../">Or click here to head back to the homepage</a>';
exit();
}
?>
I'm running most current apache2, php7, mysql on Ubuntu as a localized webserver just for the learning process. I'm still fairly new to php msql and not seeing what is causing the error. I have also ran in to an admin login loop when I click submit on the edit page button after I have logged in as the admin user instead of directing to the page to edit.
edit_page.php

Code: Select all

<?php 
session_start();
/* Created by Adam Khoury @ www.developphp.com */
include_once "admin_check.php";
?>
<?php 
// You should put an if condition here to check that the posted $pid variable is present first thing, I did not do that
$pid = preg_replace("[^0-9]", "", $_POST['pid']); // filter everything but numbers for security
// Query the body section for the proper page
include_once "../scripts/connect_to_mysql.php";
$sqlCommand = "SELECT pagetitle, linklabel, pagebody FROM pages WHERE id='$pid' LIMIT 1"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $pagetitle = $row["pagetitle"];
	$linklabel = $row["linklabel"];
	$pagebody = $row["pagebody"];
	$pagebody = str_replace("<br />", "", $pagebody);
} 
mysqli_free_result($query); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Editing Page</title>
<script type="text/javascript">

function validate_form ( ) { 
    valid = true;
    if ( document.form.pagetitle.value == "" ) { 
	alert ( "Please enter the page title." ); 
	valid = false;
	} else if ( document.form.linklabel.value == "" ) { 
	alert ( "Please enter info for the link label." ); 
	valid = false;
	} else if ( document.form.pagebody.value == "" ) { 
	alert ( "Please enter some info into the page body." ); 
	valid = false;
	}
	return valid;
}
</script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
}
-->
</style></head>

<body>
<table width="100%" border="0" cellpadding="8">
  <tr>
    <td><h3>Editing Existing Page&nbsp;&nbsp;&bull;&nbsp;&nbsp; <a href="index.php">Admin Home</a> &nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="../" target="_blank">View Live Website</a></h3></td>
  </tr>
  <tr>
    <td><?php echo $error_message; ?><br /></td>
  </tr>
  <tr>
    <td>
    
<table width="100%" border="0" cellpadding="5">
    <form id="form" name="form" method="post" action="page_edit_parse.php"  onsubmit="return validate_form ( );">
  <tr>
    <td width="12%" align="right" bgcolor="#F5E4A9">Page Full Title</td>
    <td width="88%" bgcolor="#F5E4A9"><input name="pagetitle" type="text" id="pagetitle" size="80" maxlength="64" value="<?php echo $pagetitle; ?>" /></td>
  </tr>
  <tr>
    <td align="right" bgcolor="#D7EECC">Link Label</td>
    <td bgcolor="#D7EECC"><input name="linklabel" type="text" id="linklabel" maxlength="24"  value="<?php echo $linklabel; ?>" /> 
      (What the link to this page will display as)</td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#DAEAFA">Page Body</td>
    <td bgcolor="#DAEAFA"><textarea name="pagebody" id="pagebody" cols="88" rows="16"><?php echo $pagebody; ?></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
    <input name="pid" type="hidden" value="<?php echo $pid; ?>" />
    <input type="submit" name="button" id="button" value="Submit Page Edit" /></td>
  </tr>
  </form>
</table>

    
    </td>
  </tr>
</table>
</body>
</html>
page_edit_parse.php

Code: Select all

<?php
/* Created by Adam Khoury @ www.developphp.com */

// You may want to obtain refering site name that this post came from for security purposes here
// exit the script if it is not from your site and script
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$pid = $_POST['pid'];
$pagetitle = $_POST['pagetitle'];
$linklabel = $_POST['linklabel'];
$pagebody = $_POST['pagebody'];
// Filter Function -------------------------------------------------------------------
function filterFunction ($var) { 
    $var = nl2br(htmlspecialchars($var));
    $var = eregi_replace("'", "'", $var);
    $var = eregi_replace("`", "'", $var);		
    return $var; 
} 
$pagetitle = filterFunction($pagetitle);
$linklabel = filterFunction($linklabel);
$pagebody = filterFunction($pagebody);
// End Filter Function --------------------------------------------------------------
include_once "../scripts/connect_to_mysql.php";
// Add the updated info into the database table
$query = mysqli_query($myConnection, "UPDATE pages SET pagetitle='$pagetitle', linklabel='$linklabel', pagebody='$pagebody', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));

echo 'Operation Completed Successfully! <br /><br /><a href="index.php">Click Here</a>';
exit();
?>
Thank you in advance for any help I can get on this.

thinsoldier
New php-forum User
New php-forum User
Posts: 24
Joined: Sat Dec 02, 2017 3:12 pm

Sun Dec 03, 2017 10:01 am

The tutorial you're following is either very old or very bad. Probably both.

You should focus on learning basic PHP syntax and logic first and then move straight over to using a framework where common things like logins have already been done for you.

Post Reply