creating a custom redirecting page - php

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.

Related

Session data to text file showing nothing

I have created something that once text is put inside of a text box and submit is pressed it sends this to a server and it is then stored in a session. On the press of another button you can retrieve this text and it is then transferred from the session to a txt file.
Yesterday I had this working and it was printing to my txt file but now all of a sudden it wont do it at all, can anyone spot any issues that would cause this?
This is ran when the retrieve button is pressed:
<?php session_start(); ?>
<?php
print_r($_SESSION ["input_data"]);
$myfile = fopen("TestingOut.txt", "a+") or die("Unable to open file!");
$txt = $_SESSION["input_data"];
fwrite($myfile, $txt);
fclose($myfile);
?>
I have narrowed it down to the $_SESSION["input_data"] part as it will print some random text in place for he session as seen above.
This is the creation of the session array:
<?php session_start(); ?>
<?php
$_SESSION["input_data"][] = $_POST["input"];
echo $_POST["input"];
?>
Many thanks to #marekful , I was tryign to print an array into a text file, I used implode() to fix this:
$strings = implode(" ", $_SESSION["input_data"]);
$myfile = fopen("TestingOut.txt", "a+") or die("Unable to open file!");
$txt = $strings;
fwrite($myfile, $txt);
fclose($myfile);

How to write a html file using PHP?

I used below HTML code and php code to Write new files.
<html>
<body>
<form action="write.php" method="get">
ID : <input type="text" name="ID"><br>
<input type="submit">
</form>
</body>
</html>
<?php
$myfile = fopen($_GET["ID"] , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
?>
If I submit TEST to html form , php code writes a file named "test". it hasn't a file extension.
How to write "TEST.html" with above code.?
Add the extension to the filename before opening the file:-
$filename = $_GET["ID"] . '.html';
$myfile = fopen($filename, "w") or die("Unable to open file!");
Note You are opening up yourself to all kinds of security issues here.
just use this code:-
fopen($_GET["ID"].'.html' , "w")
You need to add .html with a !empty() check there, like below:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html" , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
}
?>
To open and read this file:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html", "r") or die("Unable to open file!");
echo fread($myfile,filesize($_GET["ID"]));
fclose($myfile);
}
?>
OR:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$file = fopen($_GET["ID"]."html","r") or die("Unable to open file!"); ;
while(! feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
?>
Note:-
In my opinion you need .txt rather than .html.An HTML file with text like test really have no mean, or server any useful purpose. BTW it's up-to-you what extension you want. I just gave my opinion.

Check data before fwrite

I have simple script to get data from another server, run every 1 minutes.
<?php
$html = file_get_contents('http://google.com/data.php');
$myfile = fopen("data.html", "w") or die("Unable to open file!");
fwrite($myfile, $html);
fclose($myfile);
?>
But sometime, script write blank data to data.html. I think because connection problem between my server and remote server, or target file just blank sometime.
I wonder if i check data after get, if received data not blank then write, if data blank then exit. Please help me how to do. Thank!
file_get_contents returns false on error, so you can directly check it using === or !== as
<?php
$html = file_get_contents('http://google.com/data.php');
if($html !== false){
$myfile = fopen("data.html", "w") or die("Unable to open file!");
fwrite($myfile, $html);
fclose($myfile);
}
?>
if you want to merely test whether the output is empty, you can use for example the comparison $html !== false && $html !== ''

Trying to build a file in PHP and use fwrite to file

trying to figure out how i can make a and save a file while including default info from another file. i tried include but it did not work. any suggestion on how to build this file?
<?php
$wrtID = $_POST["fileID"];
SQL statement here to get relevant info
mkdir("CNC/$wrtID", 0770, true);
?>
<?php
$batfile = fopen("CNC/$wrtID/$wrtID.bat", "w") or die("Unable to open file!");
$txt = "
#ECHO OFF
#ECHO **** Run NC-Generator WOODWOP 4.0 ****
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sl.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sr.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-dc.mpr
#ECHO **** Done ****
";
fwrite($batfile, $txt);
fclose($batfile);
?>
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
$txt = "
include("defaultcnc.php");
if ( additional file pats needed ) {
include("component-1.php");
}
";
fwrite($slfile, $txt);
fclose($slfile);
?>
I don't see a problem in the first block of code.
On the second block, the interpreter will consider the second double quotes to mean the end of the string $txt = " include(". So everything after that would produce a PHP error.
But even if you escape those the mpr file will have the string include("defaultcnc,php"); but not the actual contents of that file. For that you should do file_get_contents("defaultcnc.php").
something like:
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
// set params to pass to defaultcnc.php
$value1 = 1;
$value2 = "I'm a text string";
$file = urlencode("defaultcnc.php?key1=".$value1."&key2=".$value2);
$txt = file_get_contents($file);
if ( additional file pats needed ) {
$txt .= file_get_contents("component-1.php");
}
fwrite($slfile, $txt);
fclose($slfile);
?>
I assume additional file pats needed means something to you. It should be a condition evaluating either true or false.

How to skip unresponding url from list of urls in php

I have list of urls in excel xheet. i am reading all urls from excel sheet and existing urls and non existing urls are storing in serapare text files.
My problem is if url is not responding then loop is stopped. my aim is to ship that url and check next url.
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("urls.xls");
$totalsheets=count($data->sheets);
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count($data->sheets[$i][cells])>0) // checking sheet not empty
{
$totalrows=count($data->sheets[$i][cells]);
for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
{
$name=$data->sheets[$i][cells][$j][1];
$url=$data->sheets[$i][cells][$j][2];
$file_headers =get_headers($url);
if(strpos($file_headers[0],"200")==true || $file_headers[0]!= 'HTTP/1.1 404 Not Found')
{
$exists = 'yes';
$myfile = fopen("existurls.txt", "w") or die("Unable to open file!");
$text .= $j." ".$name." ".$url."\n";
fwrite($myfile, $text);
fclose($myfile);
echo "Url exists";
}
else
{
$exists = 'no';
$myfile = fopen("nonexisturls.txt", "w") or die("Unable to open file!");
$text .= $j." ".$name." ".$url."\n";
fwrite($myfile, $text);
fclose($myfile);
echo "Url Not exists";
}
}
}
}
The code is working fine. But if any url is not responding then loop is stopping and not going to next one.
Please help me how to skip that not responding url and continue the loop.

Categories