Make PHP Click Link On Same PAge Or Process A Form - php

Is there any php code i can use to click a link or process a form on the page the php is on?
Im building a redirect script and what i need to do is use php to move user to next page, its mandatory that php has to be used html doesnt work. In it i have a self submit forum but it doesnt work how i load the script. Is there a way i can use php code to submit it? or remove it and put a link there then use php to click that link?
This is the code below:
if ($_GET['ref_spoof'] != NULL)
{
$offer = urldecode($_GET['ref_spoof']);
$p1 = strpos ($offer, '?') + 1;
$url_par = substr ($offer , $p1);
$paryval = split ('&', $url_par);
$p = array();
foreach ($paryval as $value)
{
$p[] = split ('=',$value);
}
//header('Location: '.$offer.'') ;
print
'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">$("#mylink").click()</script>
Index Page
<script type="text/javascript">$("#mylink").click()</script>
<script type="text/javascript">document.getElementById("myLink").click();</script>
<form action="'.$offer.'" method="get" id="myform">
';
foreach ($p as $value)
{
echo '<input type="hidden" name="'.$value[0].'" value="'.$value[1].'">';
}
echo '</form><script language="JavaScript"> document.getElementById(\'myform\').submit();</script></body></html>';
}

Looks like you're trying to make this too complicated.
You're loading a page that submits a form using GET.
Is there any reason you can't use
header("Location : ".$offer."?".http_build_query($p));
http_build_query being a function to generate an URL string from an array. Assuming $p is the array containing all form fieldnames+values.
example of http_build_query:
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
will result in:
foo=bar&baz=boom&cow=milk&php=hypertext+processor

Related

Accessing properties in an include when setting that include in to a file

I am trying to create a template html page which I will call via an include to set to a variable, this variable will then be used to set the value of a new file. I need the variables in the included file to be resolved so that the values are populated correctly.
To demo imagine these files:
main.php
$someVar = "someValue";
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
if (file_put_contents($newFileName, $fileText) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "not created";
}
aTemplate.php
<?php
return
'<!doctype html>
<html lang="en">
<head>
<title><?php echo $someVar; ?></title>
</head>
</html>'
?>
What is currently happening is that the variables stay unresolved and hold no value so in the created html file the title is:
<title></title>
Instead of
<title>someValue</title>
How can I change the 'aTemplate.php' file to resolve the properties set in 'main.php'?
Just use this at your aTemplate.php:
<?php
return '<!doctype html>
<html lang="en">
<head>
<title>'. $someVar .'</title>
</head>
</html>';
?>
There are a couple of problems with your template, firstly as you have the HTML in single quotes, this won't do any of the string substitutions. Secondly, your trying to do a PHP echo whilst in HTML in PHP. I've used Heredoc to enclose the HTML as it allows any sorts of quotes and will also do the replacements.
The substitution of the value is just replaced by adding $someVar directly into the string.
So aTemplate.php becomes...
<?php
return <<< HTML
<!doctype html>
<html lang="en">
<head>
<title>$someVar</title>
</head>
</html>
HTML;
You should echo those string in page instead of return command.
The keyword return is used inside a function while your file is not a function. The browser simply puts what's inside include file has to offer. In you case it is HTML string which should be outputted using echo command.
Also the server executes code in top to bottom and left to right. Thus the variable $someVar will be accessed in aTemplate.php file.
Use below code instead to work
main.php
$someVar = "someValue";
$file = 'aTemplate.php';
// Open the file to get existing content
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
// Write the contents back to the new file
if (file_put_contents($newFileName, $fileText) !== false)
{
echo "File created (" . basename($newFileName) . ")"; }
else {
echo "not created";
}
aTemplate.php
<!doctype html> <html lang="en"><head>
<title><?php echo $someVar;?></title>
</head>
</html>

Making An HTML Directory List

I have a form on my site where users can enter links to articles
So far... when a link is submitted, I am able to get that link to post to a destination html page.
However... if another link is submitted, it deletes the first one.
I would like the links to 'stack' and make a list to the destination (directory) page (which is currently an html page).
I don't know how to achieve this. Any advice or examples would be greatly appreciated.
I have include a very stripped down version of all three pages....
1.) The Form
<!DOCTYPE html>
<html>
<head>
<title>FORM</title>
<style>
body{margin-top:20px; margin-left:20px;}
.fieldHeader{font-family:Arial, Helvetica, sans-serif; font-size:12pt;}
.articleURL{margin-top:10px; width:700px; height:25px;}
.btnWrap{margin-top:20px;}
.postButton{cursor:pointer;}
</style>
</head>
<body>
<form action="urlUpload.php" method="post" enctype="multipart/form-data">
<div class="fieldHeader">Enter Article Link:</div>
<input class="articleURL" id="articleURL" name="articleURL" autocomplete="off">
<div class="btnWrap"><input class="postButton" type="submit" name="submit" value="POST"></button></div>
</form>
</body>
</html>
The Upload PHP (buffer) Page
<?php ob_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>urlUpload</title>
<style>body{margin-top:20px; margin-left:20px;}</style>
</head>
<body>
<?php $articleURL = htmlspecialchars($_POST['articleURL']); echo $articleURL;?>
</body>
</html>
<?php echo ''; file_put_contents("urlDirectory.html", ob_get_contents()); ?>
3.) The Destination HTML 'Directory List' page
<!DOCTYPE html>
<html>
<head>
<title>urlDirectory</title>
<style>body{margin-top:20px; margin-left:20px;}</style>
</head>
<body>
Sumbitted URL's should be listed here:
</body>
</html>
PS: I may not even need the middle php 'buffer' page. My knowledge of this sort of thing is limited thus far. If I don't need that, and can skip that page to accomplish my needs, please advise as well.
You can do this by using PHP to write the file and using urlDirectory.html as a template. You will just need to change your php file:
urlUpload.php
<?php
function saveUrl($url, $template, $tag)
{
// If template is invalid, return
if (!file_exists($template)) {
return false;
}
// Remove whitespace from URL
$url = trim($url);
// Ignore invalid urls
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return true;
}
// Read template into array
$html = file($template);
foreach ($html as &$line) {
// Look for the tag, we will add our new URL directly before this tag, use
// preg_match incase the tag is preceded or followed by some other text
if (preg_match("/(.*)?(" . preg_quote($tag, '/') . ")(.*)?/", $line, $matches)) {
// Create line for URL
$urlLine = '<p>' . htmlspecialchars($_POST['articleURL']) . '</p>' . PHP_EOL;
// Handle lines that just contain body and lines that have text before body
$line = $matches[1] == $tag ? $urlLine . $matches[1] : $matches[1] . $urlLine . $matches[2];
// If we have text after body add that too
if (isset($matches[3])) {
$line .= $matches[3];
}
// Don't process any more lines
break;
}
}
// Save file
return file_put_contents($template, implode('', $html));
}
$template = 'urlDirectory.html';
$result = saveUrl($_POST['articleURL'], $template, '</body>');
// Output to browser
echo $result ? file_get_contents($template) : 'Template error';

Data from PHP-include not shown in parent when using variable variable

I experience the following problem. By clicking the button in start.php the file fakten02.php is called with the parameter DE2 . This parameter is used as a variable variable to convert the array $DE2 into a string and display it in start.php. Unfortunately, this does not happen. If fakten02.php is directly called with the parameter, it works. If the parameter is hard-coded in fakten02.php the content of $land is shown in start.php. However, if $land is filled from $text2, $land is empty in start.php.
start.php
<!DOCTYPE HTML>
<html>
<head>
<title>Start</title>
</head>
<body>
<div id="spalten">
<button type="submit" id="land1">Klicken</button>
</div>
<?php
include("fakten02.php");
print_r($land);
?>
</body>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#spalten > button").click(function() {
$.get("fakten02.php", {sland:'DE2'});
<?php echo $land; ?>;
})
})
</script>
</html>
fakten02.php
<?php
$parameter = $_GET['sland'];
//$parameter=$_REQUEST['sland'];
//print_r($_REQUEST);
$DE2 = array("ich", "bin", "groß");
//echo "Text $text";
//$text2 = "$".$parameter;
$param2 = $parameter;
$text2 =$$param2;
for ($i=0; $i < count($text2); $i++) {
$land.="$text2[$i] "; //Does not work
$land.= "daten[$i] = '$DE2[$i]';"; //Returns expected data
}
//print( "aus 2 $land, $param2");
?>
I don't understand this behaviour. I did a lot of searching here and on Google, but I could not find a similar problem. How can I resolve this issue?

Parse/Get HTML title element from a PHP file for an Ajax call

I am trying to get the title element's content that is contained in a echo statement of a PHP file.
I am using a PHP file for a website that when accessed by a Ajax call it returns only part of the page, but when accessed directly it returns the entire page.
That much is working fine. But I would like to change the title of the page when it is accessed via the Ajax call, the innerHTML of the title tag is what I'm trying to get.
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo '
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Products at Avrent</title>
<meta http-equiv="content-type" content="text/htmlcharset=utf-8" />
With a HTML file this code works.
<?php
if(isset($_GET['url'])) {
$url = $_GET['url'];
$html = file_get_html($url);
/* get page's title */
preg_match("/<title>(.+)<\/title>/siU", $html, $matches);
$title = $matches[1];
echo $title;
}
?>
But it returns gibberish when I try using it with a PHP file.
Can someone help me find a PHP script that will work on a PHP file?
Here's what I've gathered: you have a bunch of HTML pages. You have an index.php script that takes a URL, loads up the HTML from that URL, swaps out the title, then spits the HTML back out?
First of all, why do you have things set up like that? If you insist...
You (at the very least) should do this:
index.php
Remove the RegEx. You're using an HTML parser; use that!
<?php
if(isset($_GET['url'])) {
$url = $_GET['url'];
$html = file_get_html($url);
/* get page's title */
$title = $html->find('title', 0)->innertext;
echo $title;
}
?>
ajax_page.php
Set title from variable.
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo '
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>' . $page_title . '</title>
<meta http-equiv="content-type" content="text/htmlcharset=utf-8" />
Then, from index.php:
$page_title = "INSERT THE PAGE TITLE HERE";
require "ajax_page.php";

get a text file using php and send the string to javascript

Ok, what I am trying to do is make a javascript loop of images, but first I have to get a list of the images. In javascript there is no way to directly grab this text file... http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt but it can be done eaisly in php, I am currently gettung the txt file using php, but the javascript cannot read the variable. How can I make javascript be able to read this variable. Here is what I have...
<?php
$file = "http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"; //Path to your *.txt file
$contents = file($file);
$string = implode($contents);
echo $string;
?>
<script type="text/javascript">
function prnt() {
var whatever = "<?= $string ?>";
alert(whatever);
}
</script>
You can use echo or print to write to the page in PHP.
var whatever = "<?php echo $string; ?>";
Although, if the file has line breaks in it, you will need to remove those.
Make it a bit more interesting: go ahead and split the fields and use JSON encoding. It should read directly in javascript without needing to call JSON.parse() on the client.
<?php
$lines = file_get_contents('http://...');
$lines = explode("\n",trim($lines));
foreach ($lines as &$line) {
$line = preg_split('/,? /',$line);
}
$js = json_encode($lines);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var dar = <?php echo $js; ?>;
</script>
</body>
</html>
You should also consider using a local proxy to cache the results of that file if you plan to run this frequently and especially if you are going to serve it up on a public web server somewhere. Store the file locally as "noaa_data.txt" and have a second script on a cron job (12 hours or something):
<?php
file_put_contents("/var/www/noaa_data.txt",file_get_contents("http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"));
?>

Categories