header location error redirecting - php

Hello I am trying to read a file which contain a url where I want to redirect I am using this
$file = 'test.txt';
$myfile = fopen($file, "r") or die("Unable to open file!");
$link = fread($myfile,filesize($link));
fclose($myfile);
header("Location: $link");
The browser show me an error "The Page isn´t redirect correctly"
The file test.txt contains
http://www.google.es

See this statement here,
$link = fread($myfile,filesize($link));
^^^^^^^^^^^^^^^
filesize() function expects the file path as it's argument, but you're passing an undefined variable $link.
So the above statement should be like this:
$link = fread($myfile,filesize($file));
Also, use exit(); after header(...); because header(...); itself is not sufficient to redirect the user to a different page.
header("Location: $link");
exit();

You're using the wrong variable for
filesize($link)
which should be $file
filesize($file)
(Edit):
Debugging procedure (and with error reporting set to catch and display).
Having commented out the header and echoing the $link variable
$file = "test.txt";
$myfile = fopen($file, "r") or die("Unable to open file!");
echo $link = fread($myfile,filesize($link));
fclose($myfile);
// header("Location: $link");
...you would have been presented with the following notice/warning:
Notice: Undefined variable: link in /path/to/file.php on line x
Warning: fread(): Length parameter must be greater than 0 in /path/to/file.php on line x
Since fread() couldn't figure out the filesize, it failed to to "read" it.
And of course as Rajdeep stated in his (much better) answer, to add exit; after header. Yours would have (most likely) worked, but it is indeed better practice.

Related

creating a custom redirecting page

I want to make a page like
www.example.com/redirect.php
above page must redirect to another page like
www.google.com
But whenever I open www.example.com/redirect.php?changeurl=www.yahoo.com
from now onwards www.example.com/redirect.php
page should start redirecting to yahoo.com
if changeurl=youtube.com
from now onwards
the page www.example.com/redirect.php must star redirecting to youtube.com
How can I do that without using SQL database, only by using a single file "redirect.php"
Please ask if you need more information about this question
First off you should at least have some code already written.
But try using this:
redirect.php:
<?php
if(!file_exists("redirect_url.txt")) {
$createfile= fopen("redirect_url.txt", "w") or die("Unable to open file!");
$txt = "https://www.google.com";
fwrite($createfile, $txt);
fclose($createfile);
}
if(isset($_GET['changeurl'])) {
$editfile= fopen("redirect_url.txt", "w") or die("Unable to open file!");
$txt = $_GET['changeurl'];
fwrite($editfile, $txt);
fclose($editfile);
header("Location: " . $_GET['changeurl']);
} else {
$myfile = fopen("redirect_url.txt", "r") or die("Unable to open file!");
$url = fgets($myfile);
fclose($myfile);
header("Location: " . $url);
}
?>
Also make sure in the ?changeurl You always use http:// or https://, Since you are not using a database you can use a .txt file.

Can't get my php to add to my text document?

I'm currently working on a project for college but i'm having issues with it. I have two pages with a form on each which includes three text fields (des,act,date) I'm trying to make it so that it will add to the text document the information from the forms but at the minute all it is doing is overwriting it. Anyone know how to solve this?
Page 1
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
//Send Data
$content = 'OBSERVATION'."\r\n".'Breif Description: '.$_POST['des1']."\r\n".'Agreed Action: '.$_POST['act1']."\r\n".'Close Date: '.$_POST['date1']."\r\n";
if (isset($_POST['submit'])){
$myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
fwrite($myFile, $content);
fclose($myFile);
header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
}
?>
Page 2
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
//Send Data
$content = "\r\n\r\n".'GOOD PRACTICES'."\r\n".'Breif Description: '.$_POST['des2']."\r\n".'Agreed Action: '.$_POST['act2']."\r\n".'Close Date: '.$_POST['date2']."\r\n";
if (isset($_POST['submit'])){
$myFile=fopen("Observation.txt","w") or exit("Can’t open file!");
fwrite($myFile, $content);
fclose($myFile);
}
?>
fopen() with a mode of 'w'
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
fopen() with a mode of 'a'
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
Use file_put_contents function with FILE_APPEND flag.
This function is identical to calling fopen(), fwrite() and fclose()
successively to write data to a file.
FILE_APPEND : If file filename already exists, append the data to the file instead of overwriting it.
...
if (isset($_POST['submit'])) {
file_put_contents("Observation.txt", $content, FILE_APPEND);
header( 'Location: http://www.murphy.sulmaxmarketing.com/GoodPractices.php' ) ;
exit;
}
...
http://php.net/manual/en/function.file-put-contents.php
Use file_put_content
if (isset($_POST['submit'])) {
file_put_contents("Observation.txt", $content, FILE_APPEND);
... your code here
}
Here third parameter in file_put_content "FILE_APPEND" will append your file every time with new content in your previous code it was overwrite one content with another because of same name so if you want to do it on that way than you want to set different name of both file.
Here file_put_content function url : http://php.net/manual/en/function.file-put-contents.php

Error by creating file

I got a error when creating a file using this php code:
$userdbfile = file('userfiles/' . $steamprofile['steamid'] . '.txt');
$fhuserdb = fopen($userdbfile, 'a');
fwrite($fhuserdb, $steamprofile['steamid']);
fwrite($fhuserdb, "0");
close($fhuserdb);
header("Location: index.html");
exit;
Error:
Warning: file(userfiles/76561198043436466.txt): failed to open stream: No such file or directory in /home/u294343259/public_html/admin/lw/login.php on line 7
Warning: fopen(): Filename cannot be empty in /home/u294343259/public_html/admin/lw/login.php on line 12
Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/u294343259/public_html/admin/lw/login.php on line 13
Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/u294343259/public_html/admin/lw/login.php on line 14
Fatal error: Call to undefined function close() in /home/u294343259/public_html/admin/lw/login.php on line 15
file() doesn't create a new file! It only reads file. So just remove it and use fopen(), like this
$userdbfile = 'userfiles/' . $steamprofile['steamid'] . '.txt';
$fhuserdb = fopen($userdbfile, 'a');
//checks that the file was successfully opened
if($fhuserdb) {
fwrite($fhuserdb, $steamprofile['steamid']);
fwrite($fhuserdb, "0");
fclose($fhuserdb);
}
//^ The function is 'fclose()' not 'close()' to close your file
header("Location: index.html");
exit;
Also make sure that the folder does have proper permissions to write to it.
Read the manual:
the file function reads an entire file into an array. The first warning tells you the requested file does not exist
the fopen function expects the first argument to be a string, file returns an array or false on failure. The first argument of fopen should be a string, specifying a path to a file you want to open. It returns a resource (file handle) or false on failure
fwrite expects you to pass a valid file handle, you don't check the return value of fopen (which is false in your case), so you're not writing to an actual file
close does not exist, fclose does, again: this needs to be called on a valid file handle, which you don't have, and thus this line, too, will fail
the header function can only be called if no output has been sent (read the bit below description carefully), your code is generating warnings and errors, which produce output. therefore, it's too late to call header
So what now?
Pas the path you're passing to file to fopen, check its return value and proceed accordingly:
$userdbfile = 'userfiles/' . $steamprofile['steamid'] . '.txt';
$fh = fopen($userdbfile, 'a');
if (!$fh)
{//file could not be opened/created, handle error here
exit();
}
fwrite($fh, $steamprofile['steamid']);
fwrite($fh, '0');
fclose($fh);
header('Location: index.html');
exit();

failed to open stream: Invalid argument

In this code :
$path = "C:\NucServ\www\vv\static\arrays\news.php";
$fp = fopen($path, "w");
if(fwrite($fp=fopen($path,"w"),$text))
{
echo "ok";
}
fclose($fp);
I have this error message:
failed to open stream: Invalid argument
What is wrong in my code?
Your backslashes is converted into special chars by PHP. For instance, ...arrays\news.php gets turned into
...arrays
ews.php
You should escape them like this:
$path = "C:\\NucServ\\www\\vv\\static\\arrays\\news.php";
Or use singles, like this:
$path = 'C:\NucServ\www\vv\static\arrays\news.php';
Also, your if is messed up. You shouldn't fopen the file again. Just use your $fp which you already have.
path error:
$path = 'C:/NucServ/www/vv/static/arrays/news.php';
file lock:
user file_get_contents replace fopen

PHP - failed write

I am stuck and in need of a hand. Hope someone can help?
Anyone have any idea why I am getting "failed write" in this code?
$write_file = "/usr/home/public_html/php/users_v2.sql";
$write_handle = fopen($write_file, "w") || die("Couln't open users_v2!");
if (is_writeable($write_file)) {
if ($write_handle === FALSE) echo 'Failed handle?!';
if (fwrite($write_handle, "Hi\n") === FALSE) echo "Failed write!\n";
}
fclose($write_handle);
Thanks in advance.
By using the OR operator when creating your file handle, you are returning a boolean value depending on the operation. So $write_handle will contain true or false, instead of the file resource. A better way to open a file for writing and test that it was successful would be this:
$write_handle = fopen($write_file, 'w');
if ($write_handle === false)
{
die('Could not open file ' . $write_file);
}
Additionally, you could use the file_put_contents() function which handles the fopen(), fwrite() and fclose() for you. I only recommend this if you are executing only one write to the same file, as it will be a lot of overhead, and unless you pass the FILE_APPEND flag, it will empty the file for every write.
I have seen it used everywhere but the problem is the || die("Couln't open users_v2!");
First I added:
error_reporting(E_ALL);
to see what php is reporting for errors.
$write_handle = fopen($write_file, "w") || die("Couln't open users_v2!");
fclose($write_handle);
Returns an invalid stream handle error and file handle of 1. Without it the returned file handle is "Resource id #x".
Changing the line to:
$write_handle = fopen($write_file, "w"); // || die("Couln't open users_v2!");
and your code works fine. Gonna go post this on php.net now.

Categories