Help!!

Hi im a beginner with php and also a new member of this site.

I am getting this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/blake/public_html/searchsrc/google.php:4) in /home/blake/public_html/searchsrc/google.php on line 35

Also I would like to know how to make the first write (lastsearch.txt) overwrite everything in the file instead of just putting it next to it

Heres my code:
[php]

<?
if (!$_POST['search']) {
?>
<form method="post">
Search:
<input type="text" name="search"/>
<input type="submit" />
</form>
<?
}

else {
?>
	<?php
	$myFile = "lastsearch.txt";
	$fh = fopen($myFile, 'r');
	$tosearch = fread($fh, 5);
	fclose($fh);

	$search = $_POST['search'];
	$file = "lastsearch.txt";
	$filename = fopen($file, 'a') or die("can't open file");
	fwrite($filename, $search."\r");
	fclose($filename);
	$ip= $_SERVER['REMOTE_ADDR'];
	$search = $_POST['search'];
	$file = "searchlog.txt";
	$filename = fopen($file, 'a') or die("can't open file");
	$date = date('j/n/y h:i:S A');
	fwrite($filename, $ip." - ".$date." - ".$search."\r\n");
	fclose($filename);
	header("Location: http://google.com/search?q=$tosearch");
}
?>
[/php]

Thanks

For your second question - to overwrite file content instead of appending - use the “w” flag instead of “a”, so that your command looks like this:
[php]fopen($file, ‘w’)[/php]

As for the warning message - you need to change structure of your code so that call to the header() function is above any output to browser, including etc.

But I want to redirect after doing writing, Is there another way to do it

What this basicly is is a random search

So when the user types somthing and presses search, it searches what the person before them saw
and writes what they typed in the box to a text file for the next user

So if i have the header at the top, then the rest of the code wont be executed first

You need to change your code structure. In your code logic - if user pressed the “search” button, you are doing some processing (writing to file, etc.) and then redirecting to another page. No any output to the browser at all in this case. How to modify your code? Here is a brief code to give you idea:
[php]<?php
if($_POST[“search”]){
// do data processing, write to file, redirect…
header(“Location: http://google.com/search?q=$tosearch”);
}
?>

Search: [/php]

Heres my code now
[php] <?php
if($_POST[“search”]){
$myFile = “lastsearch.txt”;
$fh = fopen($myFile, ‘r’);
$tosearch = fread($fh, 5);
fclose($fh);

	$search = $_POST['search'];
	$file = "lastsearch.txt";
	$filename = fopen($file, 'w') or die("can't open file");
	fwrite($filename, $search."\r");
	fclose($filename);
	$ip= $_SERVER['REMOTE_ADDR'];
	$search = $_POST['search'];
	$file = "searchlog.txt";
	$filename = fopen($file, 'a') or die("can't open file");
	$date = date('j/n/y h:i:S A');
	fwrite($filename, $ip." - ".$date." - ".$search."\r\n");
	fclose($filename);
header("Location: http://google.com/search?q=$tosearch");

}
?>

Search: [/php]

Now im getting

Warning: Cannot modify header information - headers already sent by (output started at /home/blake/public_html/searchsrc/google.php:1) in /home/blake/public_html/searchsrc/google.php on line 20

One thing missing - add exit aftert the header() function call:
[php]header(“Location: http://google.com/search?q=$tosearch”);
exit;[/php]

Also, make sure you have no any single char (even space!) before opening php tag <?php

Thanks everyone its working now, but i did notice that if its over 5 characters it cuts off the rest. Is this why?

$tosearch = fread($fh, 5);

What do i change it to to make it unlimited

Try it if you want at http://blake.org.nz/search

http://blake.org.nz/mystery-search actually

Php Coder, I did it without the end and it still worked, see http://blake.org.nz/search

Why isnt there an edit button, I mean exit not end

The Edit button is disabled for new members to prevent spam. It will appear for you once you reach certain amount of posts. Until that please keep previewing your post before sending.

Sponsor our Newsletter | Privacy Policy | Terms of Service