[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Why is my $_GET variable coming up undefined??

Hi, I set 'clear' in my link:

<a href="logfile.php?clear=true">Clear Log File</a>

I try to get it:

if($_GET['clear'] == 'true') {
      file_put_contents($logfile, '');
      // Add the first log entry
      log_action('Logs Cleared', "by User Id {$session->user_id}");
      // redirect to this same pages so that the URL won't have "clear=true" anymore
      ob_end_clean();
      redirect_to('logfile.php');
}

But for some reason thats not working??  Anyone know why?

Notice: Undefined index: clear in /hermes/web09c/b2950/moo.auroriellacom/logfile.php on line 11
<?php
ini_set('display_errors' ,1); 
error_reporting(E_ALL);

require_once 'includes/library.php';
$title = "Auroriella Management";
include 'header.php';

$logfile = 'logs/logs.txt';

if($_GET['clear'] == 'true') {
	file_put_contents($logfile, '');
	// Add the first log entry
	log_action('Logs Cleared', "by User Id {$session->user_id}");
	// redirect to this same pages so that the URL won't have "clear=true" anymore
	ob_end_clean();
	redirect_to('logfile.php');
}

//validate_fields(); // Validation
find_selected_id(); // Needs to go below process_fields to show updates
echo nav($select_nav);

include 'sidebar.php';
echo menu($select_menu, $select_product, $select_metal);

include 'display.php';
//user_info();
?>
<a href="logfile.php?clear=true">Clear Log File</a>
<?php
echo "<p>";
if(file_exists($logfile) && is_readable($logfile) && $handle = fopen($logfile, 'r')) {
	echo "<ul class=\"logs\">";
	while(!feof($handle)) { // Until we get to the end of the file
		$entry = fgets($handle); // fgets works bc we know there are new lines
		if(trim($entry) != "") { // Trim white space and make sure its not nothing
			echo "<li>{$entry}</li>";
		}
	}
	echo "</ul>";
	fclose($handle);
}
else {
	echo "Could not read from {$logfile}.";
}
echo "<p>";

require 'footer.php';
?>

Open in new window

0
FairyBusiness
Asked:
FairyBusiness
3 Solutions
 
Jagadishwor DulalBraces MediaCommented:
Try using if(isset($_GET['clear']==true)) so that your can find the $_GET method have set a value to 'clear' or not
0
 
AielloJCommented:
FairyBusiness:

Your code looks OK.  Try using PHP's print_r() function before line 11 to see what's in the $_GET associative array.

  print_r($_GET);

AielloJ
0
 
Lukasz ChmielewskiCommented:
try using
if($_GET['clear'] == true)
But using  boolean values at the top of the script is somehow tricky. Set your clear=1 instead of true
and check for if($_GET
== 1)
0
 
hernst42Commented:
If you call logfile.php without any parameter $_GET is empty and thus you get the warning.

If you want to eliminate the notices use as check:
if(isset($_GET['clear']) && $_GET['clear'] == 'true') {
0
 
FairyBusinessAuthor Commented:
Thanks you guys! I had to end up doing this:

if(isset($_GET['clear']) && $_GET['clear'] == 1) {
      file_put_contents($logfile, '');
      // Add the first log entry
      log_action('Logs Cleared', "by User Id {$session->user_id}");
      // redirect to this same pages so that the URL won't have "clear=1" anymore
      ob_end_clean();
      redirect_to('logfile.php');
}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now