I have issue to redirect another location after fopen() function in php. below is my function which i m using.
<?php
function create_file($filename){
$my_file = 'folder/index.php';
$fh = fopen($my_file, "wb");
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Some data</title>
</head>
<body>Here some data</body>
</html>';
fwrite($fh, $data);
fclose($fh);
return true;
ob_end_clean();
exit();
}
$file = create_file(test);
if($file == true){
$url = 'http://example.com';
return $url;
}
else{
return 0;
}
?>
If you want to redirect in PHP, you cannot send any output to the server before header("Location: http://example.com");.
This includes any HTML, text or white spaces that aren't wrapped in a PHP tag set.
The header function must be called before any page output (if HTML has already been displayed, it's too late for your headers!).
Related
I'm wanting to set up a loop or maybe a page refresh that pings my server over and over again and tells me the milliseconds.
This is the code I'm using but not sure how to make it keep refreshing and giving me the response live. Can someone show me how to make it live so it constantly updates every 1 second or even every 2 or 3 seconds is fine also. Just need it to be live.
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
Edited with Paul's Code still no luck:
<!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;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
</body>
</html>
There are two ways I can think of:
1.Setting up refresh attribute in the page header
<!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;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
//your php code here
?>
</body>
</html>
2.Use a crontab job to execute this command every second:
w3m http://yourhost/yourPage.php
I think the first solution is closer to what you need.
I'm having a problem with file_get_contents and fwrite.
To get a script to work I have to print content from an external URL into a html file.
I'm using this code:
<!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" />
</head>
<body>
<?php
$url = 'http://www.vasttrafik.se/nasta-tur-fullskarm/?externalid=9021014005135000';
$content = file_get_contents($url);
echo $content; // Actually writes out correct
$myFile = "response.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $content); // Doesn't write out correct ???
fclose($fh);
?>
</body>
</html>
When I echo out the file_get_contents, the HTML shows up nicely (with the Swedish special characters: åäö)
However.. The file "response.php" shows bad characters instead of åäö.
Any ideas? Does the fwrite use another encoding?
Thanks!
UPDATE!
Solved with this:
$content = "\xEF\xBB\xBF";
$content .= utf8_encode(file_get_contents($url));
SOLVED!
I needed to ad a BOM (Byte Order Mark) AND utf8_encode.
Like this:
$content = "\xEF\xBB\xBF";
$content .= utf8_encode(file_get_contents($url));
I'm trying to create a page what will log a plethora of user data for security purposes but am am unsure how best to do so.
Ideally I want all important stats for example their IP, their X-Forward IP, their browser, operating system and any other details I can get.
I've also got problems showing the variables in a php / html page as shown below:
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
$template = '<!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>Title</title>
</head>
<body>
IP: <?= $ip ?>
IP: [IP]
</body>
</html>';
$template = str_replace('[IP]', $ip, $template);
echo $template;
?>
or
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
Try something simple first:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
For a full list of SERVER superglobal variables, check this link http://php.net/manual/en/reserved.variables.server.php
<?php
$data = $_SERVER;
$IP = $data['REMOTE_ADDR'];
echo "IP address is:" . $IP;
?>
There you go... now look up all the available $_SERVER values and use the ones you need.
Download link:
Download php file
download.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Home</title>
</head>
<body>
<h1>PHP file</h1>
<?php
function get_text($text)
{
....
}
function get_time($time)
{
....
}
$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];
$xml = simplexml_load_file($url);
if (count($xml))
{
foreach($xml->book as $book)
{
echo ....
}
}
?>
</body>
</html>
The download.php is a ready made API php script to provide webmasters upload to their FTP. Webmasters can be choose many options(e.g: download.php?words=2000&sort=popular&type=xml) from a form, then submit the form to get their custom API script.
This is the line that will replace the options after they submit the form.:
$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];
This is the code to force download. But i don't know how to wrap whole page with $content = "";. I know how to wrap the HTML codes but how to wrap the PHP function and codes on the page?
header("Content-Disposition: attachment; filename="download.php");
print $content;
Not sure if that is what yo want; but you could create a second script that calls the first one, gets the output, and sends that with the mentioned headers:
<?php
$words = (int) $_GET['words'];
$sort = $_GET['sort'];
$url = sprintf("http://localhost/wherever/download.php?words=%d&sort=%s&type=xml", $words, $sort);
$content = file_get_contents($url);
header("Content-Disposition: attachment; filename="download.php");
print $content;
?>
Call that file "force_download.php" and let the users call Download php file instead.
I have been racking my brains over this for hours, I need to pass some html to a function and have it replace the links and the return the html with the replaced links.
<?php
final static public function replace_links($campaign_id, $text) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $text, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
if(substr($match[2], 0, 2) !== '##') { // ignore the placeholders
if(substr($match[2], 0, 6) !== 'mailto') { // ignore email addresses
// $match[2] = link address
// $match[3] = link text
$url = "http://xxx.com/click?campaign_id=$campaign_id&email=##email_address##&next=" . $match[2];
#$text .= str_replace($match[2], $url, $text);
#echo $links . "\n";
preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
}
}
return $text;
}
}
}
?>
When I echo the links it shows all the matched links. Question is how do i return the complete HTML with the replaced links example below.
<!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>Untitled Document</title>
</head>
<body>
xxxx
xxxx
xxxx
</body>
</html>
Should become:
<!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>Untitled Document</title>
</head>
<body>
xxxx
xxxx
xxxx
</body>
</html>
Hope this makes sense.
Thanks in advance,
Kyle
Don't reinvent the wheel just use something like this:
http://code.google.com/p/jquery-linkify/
It's really easy i use it as well
I don't know much about preg_replace, but i needed exatcly the same function as you.
Changing the line:
preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
for this:
str_replace($match[0],$url,$text);
seems to do the trick.
I just needed to get the return from this functions, so:
//$text = preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
$text = str_replace($match[0],$url,$text);