Url and title with php snippet - php

I read several similar posts but I don't see my fault.
index.php looks like:
<head>
<title>Demo Title</title>
</head>
<body>
<?php
require_once "footer.php";
?>
</body>
footer.php looks like:
<?php
/*
* _$ Rev. : 08 Sep 2010 14:52:26 $_
* footer.php
*/
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo $url;
$file = # fopen($_SERVER[$url],"r") or die ("Can't open HTTP_REFERER.");
$text = fread($file,16384);
if (preg_match('/<title>(.*?)<\/title>/is',$text,$found)) {
$title = $found[1];
} else {
$title = " -- no title found -- ";
}
?>
A request for the URL http://127.0.0.1/test/index.php results in:
http://127.0.0.1/test/index.phpCan't open HTTP_REFERER.
or for http://127.0.0.1/test/
http://127.0.0.1/test/Can't open HTTP_REFERER.
Any hints appreciated.

$_SERVER is an array which contains a bunch of fields relating to the server config. It does not contain an element named "http://".$host.$param, so trying to open that as a filename will result in the fopen call failing, and thus going to the die() statement.
More likely what you wanted to do was just open the file called "http://".$host.$param. If that's what you want, then just drop the $_SERVER[] bit and it should work better.
Note that because it's a URL, you will need your PHP config to allow opening of remote files using fopen(). PHP isn't always configured this way by default as it can be a security risk. Your dev machine may also be configured differently to the system you will eventually deploy to. If you find you can't open a remote URL using fopen(), there are alternatives such as using CURL, but they're not quite as straightforward as a simple fopen() call.
Also, if you're reading the whole file, you may want to consider file_get_contents() rather than fopen() and fread(), as it replaces the whole thing into a single function call.

try this:
$file = # fopen($url,"r") or die ("Can't open HTTP_REFERER.");

Try
<?php
$dom = new DOMDocument();
$host = $_SERVER['SERVER_NAME'];
$param = $_SERVER ['REQUEST_URI'];
$url = "http://".$host.$param;
echo 'getting title for page: "' . $url . '"';
$dom->loadHTML($url);
$dom->getElementsByTagName('title');
if ($dom->length)
{
$title = $dom->item(0);
echo $title;
}
else
{
echo 'title tag not found';
}
?>

I can see your trying to track the referral's title
You need to use $_SERVER['HTTP_REFERER']; to get that
what you want to do is something like this
$referrer = (!empty($_SERVER['HTTP_REFERER']) && !substr($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_REFERER'] : false);
if($referrer)
{
try
{
if(false !== ($resource = fopen($referrer,"r")))
{
$contents = '';
while($contents .= fread($resource,128))
{}
if(preg_match('/<title>(.*?)<\/title>/is',$contents,$found))
{
echo 'Referrer: ' $found[1];
}
}
}catch(Exception $e){}
}

Related

Value NULL after program compile in browser

I'm making an application to remove additive or commonly called Stemming confix stripping. I wanted to make a loop to process stemming in each text file. process stemming I've put them in the loop. making the process the content of each document also I put in a text file. when I run in the browser no error but only the note NULL. what's the solution? I attach my program and program results`it is code
<?php
require_once __DIR__ . '/vendor/autoload.php';
$array_sentence = glob('../../ujicoba/simpantoken/*.txt');
settype($array_sentence, "string");
if(is_array($array_sentence) && is_object($array_sentence))
{
foreach ($array_sentence as $text)
{
$textnormalizer = new src\Sastrawi\Stemmer\Filter\TextNormalizer();
$stemmerFactory = new \Sastrawi\Stemmer\StemmerFactory();
$stemmer = $stemmerFactory->createStemmer();
$content = file_get_contents($text);
$stemmer = $stemmerFactory->createStemmer();
$output = $stemmer->stem(array($content));
echo $output . "\n";
}
}
var_dump($content);
?>
<!DOCTYPE html>
<html>
<head>
<title>Confix Stripping Stemmer</title>
</head>
<body>
</body>
</html>
my source code result in browser when running programenter code here
On l.4, settype($array_sentence, "string"); force $array_sentence as a string, which means is_array($array_sentence) && is_object($array_sentence) will return false.
The following code works for stemming:
<?php
include('stopword.php');
$regexRules = array(
'/^be(.*)lah$/',
'/^be(.*)an$/',
'/^me(.*)i$/',
'/^di(.*)i$/',
'/^pe(.*)i$/',
'/^ter(.*)i$/',
'/^di(.*)kan$/',
'/^di(.*)nya$/',
'/^di(.*)kannya$/',
'/^mem(.*)pe$/',
'/^meng(.*)g$/',
'/^meng(.*)h$/',
'/^meng(.*)q$/',
'/^meng(.*)k$/',
'/^mem(.*)kan$/',
'/^diper(.*)i$/',
'/^di(.*)i$/',
'/^memper(.*)kan$/',
'/^meny(.*)i$/',
'/^meny(.*)kan$/',
'/^men(.*)kan$/',
'/^me(.*)kan$/',
'/^meng(.*)nya$/',
'/^memper(.*)i$/',
'/^men(.*)i$/',
'/^meng(.*)i$/',
'/^ber(.*)nya$/',
'/^ber(.*)an$/',
'/^ke(.*)an$/',
'/^ke(.*)annya$/',
'/^peng(.*)an$/',
'/^peny(.*)an$/',
'/^per(.*)an$/',
'/^pen(.*)an$/',
'/^pe(.*)an$/',
'/^ber(.*)$/',
'/^di(.*)$/',
'/^men(.*)$/',
'/^meng(.*)$/',
'/^meny(.*)$/',
'/^mem(.*)$/',
'/^pen(.*)$/',
'/^peng(.*)$/',
'/^ter(.*)$/',
'/^mem(.*)$/',
'/^(.*)nya$/',
'/^(.*)lah$/',
'/^(.*)pun$/',
'/^(.*)kah$/',
'/^(.*)mu$/',
'/^(.*)an$/',
'/^(.*)kan$/',
'/^(.*)i$/',
'/^(.*)ku$/',
);
global $regexRules;
$file_string = glob('yourfoldertoseavedata_text/*.txt');
$string = array('(.*)');
foreach ($file_string as $data)
{
$string[] = file_get_contents($data);
$stemming = str_ireplace($regexRules,"", $string);
var_dump($stemming);
}
?>

How can I replace braces with <?php ?> in php file?

I wanna replace braces with <?php ?> in a file with php extension.
I have a class as a library and in this class I have three function like these:
function replace_left_delimeter($buffer)
{
return($this->replace_right_delimeter(str_replace("{", "<?php echo $", $buffer)));
}
function replace_right_delimeter($buffer)
{
return(str_replace("}", "; ?> ", $buffer));
}
function parser($view,$data)
{
ob_start(array($this,"replace_left_delimeter"));
include APP_DIR.DS.'view'.DS.$view.'.php';
ob_end_flush();
}
and I have a view file with php extension like this:
{tmp} tmpstr
in output I save just tmpstr and in source code in browser I get
<?php echo $tmp; ?>
tmpstr
In include file <? shown as <!--? and be comment. Why?
What you're trying to do here won't work. The replacements carried out by the output buffering callback occur after PHP code has already been parsed and executed. Introducing new PHP code tags at this stage won't cause them to be executed.
You will need to instead preprocess the PHP source file before evaluating it, e.g.
$tp = file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$tp = str_replace("{", "<?php echo \$", $tp);
$tp = str_replace("}", "; ?>", $tp);
eval($tp);
However, I'd strongly recommend using an existing template engine; this approach will be inefficient and limited. You might want to give Twig a shot, for instance.
do this:
function parser($view,$data)
{
$data=array("data"=>$data);
$template=file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$replace = array();
foreach ($data as $key => $value) {
#if $data is array...
$replace = array_merge(
$replace,array("{".$key."}"=>$value)
);
}
$template=strtr($template,$replace);
echo $template;
}
and ignore other two functions.
How does this work:
process.php:
<?php
$contents = file_get_contents('php://stdin');
$contents = preg_replace('/\{([a-zA-Z_][a-zA-Z_0-9]*)\}/', '<?php echo $\1; ?>', $contents);
echo $contents;
bash script:
process.php < my_file.php
Note that the above works by doing a one-off search and replace. You can easily modify the script if you want to do this on the fly.
Note also, that modifying PHP code from within PHP code is a bad idea. Self-modifying code can lead to hard-to-find bugs, and is often associated with malicious software. If you explain what you are trying to achieve - your purpose - you might get a better response.

if file == index.php

I would just like to hear if it is possible to echo something on one page, and not every other page, even though they all get content from another file.
I have 3 pages, and they all have this in code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
include_once($path);
?>
If I want "index.php" to echo something that "about.php" does not echo, can you then do it?
As the title says, I'd guess on something like this:
if filename == index.php then
echo=hello
I wouldn't make it so specific If you add more and more pages what will you do? Handle them as special cases one by one? Now you have to handle two pages, after three months they'll be 10...and so on.
It's not a perfect solution but as starting point you may use a flag. Beginning like this:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
$helloWorld = TRUE;
include_once($path);
?>
And in your index.php include file:
if (helloWorld === TRUE) // or if (!!helloWorld)
echo "Hello world";
if (strpos($_SERVER['SCRIPT_NAME'],'index.php') !== false) {
echo "do stuff";
}

Open more links and then parse some elements in php

I have simple problem.
I need to open for example this page http://www.50states.com/ in php.
Then I need to open every state and from page of state for (example: Alabama), I need to parse name of state, capital, location. I want to do it with simple html dom library http://simplehtmldom.sourceforge.net/
And I want to do it on every state. How it is possible???
//parser.php
include simple_html_dom.php;
$document = file_get_html($site);
foreach($document->find('a') as $e) {
echo $e->href . '<br>';
}
So what now? Please help. I think now I need only hrefs with states and then I must open it... So?
Why not feed it with the state name on the url? Consider this as an example:
<?php
include 'simple_html_dom.php';
$state = 'alabama';
$main_url = 'http://www.50states.com/' . $state . '.htm';
$html = file_get_html($main_url);
$state_info = null;
$capital_city = '';
foreach($html->find('ul[class=bulletedList]') as $key => $value) {
$state_info = $value;
// Get particular value (traverse DOM)
// Sample: search for capital city
if(strpos($value->children(0)->children(0)->innertext, 'Capital City:')) {
$capital_city = $value->children(0)->children(0)->innertext;
}
}
echo $state_info;
echo $capital_city;
?>
For more in-depth information, you should check out the manual, its pretty well documented anyways.
http://simplehtmldom.sourceforge.net/manual.htm

Problem with include on my Drupal

I have a problem with 3rd-party-system integration in my drupal site.
Sorry for my english, i'm from russia, but i will try to explain my problem well.
Integration idea:
2 .php files
2 php-script lines (include
function's)
The problem is:
this scripts call to outside perl
(.pl) script. Perl script read the
parameters (parameters transfers by
url) and generate content.
I can't see this perl script, but i
know - hes working, but not in my
page :)
2 php files:
spectrum_view.php
<?php
$url = "http://young.spectrum.ru/cgi-bin/programs_view.pl";
$param = $_GET;
if (!empty($param))
{
$url .= "?";
foreach ($param as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
} echo $content = file_get_contents($url);
?>
spectrum_form.php
<?php
$url ="http://young.spectrum.ru/cgi-bin/programs_form.pl";
$params = $_GET;
if (!empty($params))
{
$url .= "?";
foreach ($params as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
} echo iconv("windows-1251","utf-8",(file_get_contents($url)));
?>
and the 2 php-lines, wich i insert in my drupal pages
(the first i insert in page http://new.velo-travel.ru/view
and the second in the right block)
include("http://new.velo-travel.ru/themes/themex/spectrum_view.php?$QUERY_STRING");
include("http://new.velo-travel.ru/themes/themex/spectrum_form.php?act=/view$QUERY_STRING");
So, i solved this problem, but not in drupal - only on my Localohost, i just create a 2 page:
form.php:
<?php
$url ="http://young.spectrum.ru/cgi-bin/programs_form.pl";
$params = $_GET;
if (!empty($params)){
$url .= "?";
foreach ($params as $keys=>$value) $url .= "&".$keys."=".urlencode($value);
}
$content = file_get_contents($url);
print $content;
**require_once 'view.php';**
?>
view.php:
<?php
$url = "http://young.spectrum.ru/cgi-bin/programs_view.pl";
$param = $_GET;
if (!empty($param))
{
$url .= "?";
foreach ($param as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
}
$content = file_get_contents($url);
print $content;
?>
=(
I'm not entirely sure, as to what you are trying to do. But it seems like you want to generate this content from the perl script. If this is a special page with it's own template, you should move all this code into template.php. This file is made to hold some logic you want to create the content for your page.
Personally I would prefer to make a module to handle all this, but it's probably easier to do this in the theme, with what you got now. It seems like you are making a form, and some content based on the form. This could be done in a module. You could create a Drupal form, and then handle the validation with drupal, and jst submit the data to perl. But if you would want to get it from perl, going with the theme is probably best. So how do you do it?
Implement a preprocess function for the tpl.php file you use.
Create all the logic here, you could copy the php files you use over or just include them. Import, assign the result to a variable the will be accessible in the template file.
Print the variable in your template.
In code this would look something like this:
//template.php file
function mytheme_preprocess_somename(&$vars) {
include('php');
// Do some logic.
$vars['form'] = $result_a;
$vars['my_content'] = $result_b;
}
// your .tpl.php
// Some markup here
<div><?php print $my_content; ?></div>
<div><?php print $form; ?></div>
Now, I'm not sure exactly what you are after, but something like this should help you along. Note it's important what you call your variables inside the template file, as you can overwrite some Drupal variables like $content, which can cause some bugs.
You probably are running into a security issue. Please note allow_url_fopen and allow_url_include - these settings must have accordant settings in your php.ini. Otherwise you can't e.g. include a remote file for security reasons.

Categories