php script to get URL visited from browser via PHP - php

I want to write a simple php script to get URL visited from browser via PHP with:
$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]
It should look something like this:
<?php
...some stuff to print out the variable...
?>

Choose one or use some vars did you needed from $_SERVER vars:
foreach($_SERVER as $key => $value){
echo '$_SERVER[\''. $key .'\'] => '. $value .'<br /><br />';
}
above will displaying all value.
maybe you need create new db table with column ip and url_visited.

Related

View included by $this->include does note recieve passed params

Is possible pass parameter to view when i use $this->include method in another view?
In example:
<?php
foreach ($destaques as $camping) {
$this->include('partial', ['camping' => $camping])
}
?>
But partial.php dont recieve $camping value.
When using $this->include you're making an echo of a view into an other one. So by default, the view you're loading will have acces to any data you gave to the parent view but not variables you declared into it.
A few options in your case :
Using the view method :
foreach ($destaques as $camping) {
echo view('partial', ['camping' => $camping]);
}
Moving your foreach loop in the partial view so you'll use $destaques into it.
<?php
// dont forget to echo
echo $this->include('partial')
?>
// or this way with short tags enabled
<?= $this->include('partial') ?>
And just embed your partial view in your previous foreach loop
foreach ($destaques as $camping) {
// whatever your partial view is
}
Hello I try using the parameter option. I test code and it s working. But note the include view layout method work like php default include function and it does not display until you echo it.
Here is my code which I use to test yours and it worked for me. Check it
$inc = '';
foreach ($destaques as $camping) {
$inc .= $this->include('partial', ['camping' => $camping])
}
echo $inc;
And this displayed and worked for me check it. if this is not what you were expecting, just call my attention

Simple PHP proxy with persistent URL

I have the simple PHP script:
<?php
$url = $_REQUEST['url'];
if (preg_match('/\b(https?|ftp):\/\/*/', $url) !== 1) die;
echo (file_get_contents($url));
?>
I am trying to echo the page:
http://forum.bodybuilding.com/showthread.php?t=162984431&page=10
but the echo shows:
http://forum.bodybuilding.com/showthread.php?t=162984431
example:
http://www.kylesbox.com/forumbuddy/fetch/fetch.php?url=http://forum.bodybuilding.com/showthread.php?t=162984431&page=10
I am not a PHP expert but I think this has something to do with persistent URLs? How would I go about fixng this so the echo displays everything after the & symbol as well? I do have cURL installed on my server if that helps. Thanks!
Here the "&" sign is part of query string element. So it will avoid to get value from first "&". We can two more lines on your script to get the work done.
<?php
$query=$_SERVER['QUERY_STRING']; //get the full query string in url
$query_arr=explode("url=",$query); //split the string by first get key
$url = $query_arr[1]; //take second parameter as url to be loaded
if (preg_match('/\b(https?|ftp):\/\/*/', $url) !== 1) die;
echo (file_get_contents($url));
?>
The she script available at following url as working script.
http://sugunan.net/demo/fetch.php?url=http://forum.bodybuilding.com/showthread.php?t=162984431&page=10

Variable inside a php echo function

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.

Remove a value from a querystring

I have a URL in which a querystring is produced by a PHP script. Various values are displayed in the querystring.
Basically, I need to remove a specific value from the query string when a visitor clicks on a link or a 'remove' button.
So, the querystring looks like this:
http://www.foo.com/script.php?bar1=green&bar2=blue
But when a link or 'remove' button is clicked by a user, bar1=green is removed, and the visitor is directed to the following URL:
http://www.foo.com/script.php?bar2=blue
I thought this would be easy using basic HTML with a form or anchor but I haven't been able to do it so far.
Just so you know, i do not have access to the code on the PHP script itself; it is hosted remotely and is called to my webpage by a PHP wrapper using an iframe.
Any suggestions greatly appreciated.
Many thanks,
Matt
You can remove the value from the query string using this code:
<?php
function parseQueryString($url,$remove) {
$infos=parse_url($url);
$str=$infos["query"];
$op = array();
$pairs = explode("&", $str);
foreach ($pairs as $pair) {
list($k, $v) = array_map("urldecode", explode("=", $pair));
$op[$k] = $v;
}
if(isset($op[$remove])){
unset($op[$remove]);
}
return str_replace($str,http_build_query($op),$url);
}
echo parseQueryString( "http://www.foo.com/script.php?bar1=green&bar2=blue","bar2");
?>

URL as a Variable in PHP

I have a cool project where I need to upload an image via php/my_sql. That I can handle, but the images need to be linking to a certain url out of 100. In php can I save a url as a variable, then allow a drop-down menu of the 100 choices which point to a variable with a url?
The best choice would be to use an array:
$urls = array("url","url2","url3");
After you add all the 100 URLs in there, you can recurse through the array and output options into the tag.
<?php
echo "<select>";
foreach($urls as $current_url){
echo "<option>" . $current_url . "</option>";
}
echo "</select>";
?>
That would go through the array, echoing all the URLs into the tag.
If you don't want to set the text in the dropdown to the actual URL, you could set the array using keys array("This URL" => "url") etc. and put the URL value into the "value" property of the tag, and using the key name as the value between the opening and closing tags of the list.
If you need an explanation of that as well, I can provide one.
I'm still not sure what you mean, but if you want to know how to store a url in a variable that is usually done in a string like this:
$url = "http://www.mysite.com/the/beautiful/image.gif";
You can also redirect to that url like this:
header('Location: '.$url);
die();
If you want the user to decide to which site to go, do it similar to what BraedenP posted:
<select id="urls" onchange="document.location.href=document.getElementById('urls').options[document.getElementById('urls').selectedIndex].value;">
<?php
$urls = array(
'Image One' => 'http://www.mysite.com/one.gif',
'Image Two' => 'http://www.mysite.com/two.gif',
'Image Thee' => 'http://www.mysite.com/three.gif'
);
foreach($urls as $name=>$url){
echo "<option value=\"{$url}\">{$name}</option>";
}
?>
</select>

Categories