Simple PHP proxy with persistent URL - php

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

Related

Replace Content on Page Based on URL Parameter with PHP

I'd like to replace content within my page based on the URL parameter.
Ideally I'd like to use PHP to get:
if {{parameter is X}} display {{content X}}
if {{parameter is Y}} display {{content Y}}
..for a few pages.
Current set up:
<?php if ($CURRENT_PAGE == "Index") { ?>
<div id="firstDiv">this is the standard page</div>
<?php } ?>
<?php if ($CURRENT_PAGE == "p1") { ?>
<div id-"secondDiv">this is a variation of the page</div>
<?php } ?>
And using include("includes/content.php"); to call the html blocks to the page
The firstDiv displays in index.php as expected, but adding the URL parameter changes nothing - the same div still shows (I'd like it to be replaced with the secondDiv)
It seems $CURRENT_PAGE doesn't like URL parameters - what is the alternative?
Hopefully this makes sense, I'm pretty new to PHP. Happy to provide more details if required.
Thanks in advance for any help.
-- UPDATE --
Thank you for the answers so far!
It seems I missed part of my own code (Thanks to vivek_23 for making me realise this - I'm using a template, excuse me!!)
I have a config file that defines which page is which, as so:
<?php
switch ($_SERVER["SCRIPT_NAME"]) {
case "index.php/?p=1":
$CURRENT_PAGE = "p1";
break;
default:
$CURRENT_PAGE = "Index";
}
?>
Before I learn $_GET, is there a way I can use my current set up?
Thanks again.
-- UPDATE 2 --
I have switched to using the $_GET method, which seems to be working well so far. My issue now is when the parameter is not set it is giving an undefined error. I'll try to remember to update with the fix.
$p = ($_GET['i']);
if($p == "1"){
echo '<div id="firstDiv"><p>this is the first div</p></div>';
}
Thanks to the two answerers below who suggested using $_GET
You can used $_GET like
if($_GET['p']==1){
echo '<div id="firstDiv">this is the standard page</div>';
}else if($_GET['p']==2){
echo '<div id="secondDiv">this is a variation of the page</div>';
}
The other way! you can used basename() with $_SERVER['PHP_SELF']
//echo basename($_SERVER['PHP_SELF']); first execute this and check the result
if(basename($_SERVER['PHP_SELF']) == 'index'){
echo '<div id="firstDiv">this is the standard page</div>';
}else{
echo '<div id="secondDiv">this is a variation of the page</div>';
}
You need to send the parameters on the URL query string, like:
yourdomain.com?p=1
So, with this URL, the query string is "?p=1", where you have a GET parameter named 'p' with a value of '1'.
In PHP to read a GET parameter you can use the associative array $_GET, like this:
$current_page = $_GET['p'];
echo $current_page; // returns '1'
The rest of your logic is OK, you can display one div or the other based on the value of the p parameter.
You can read more about how to read query string parameters here: http://php.net/manual/en/reserved.variables.get.php

Post url in form with get method

I have a problem today!
I am trying to post a URL in form via GET method
When I post URL it automatically converts to http://example.com/?url=http%3A%2F%2Fanonylinq.com%2F%3Fi%3Dphpphp from http://anonylinq.com/?i=phpphpIs there any way to solve this problem? I am doing this via PHP.
because I want to echo "i" as - <?php echo $i; ?> Everything else is done but I am stuck at this point.
Already done this too -
$urlSplitted = explode('?i=', $_GET['url']); $i = $urlSplitted[1];
if you want to go this road:
$urlSplitted = explode('?i=', $_GET['url']); $i = $urlSplitted[1];
you should use
$urlSplitted = explode('%3Fi%3D', $_GET['url']); $i = $urlSplitted[1];
Take a look at http://php.net/manual/en/function.urldecode.php
That should do the trick for it.
e.g.
$string = $_GET['url'];
$decoded = urldecode($string);
$urlSplitted = explode('?i=', $decoded );
$i = $urlSplitted[1];
I have done this:
Just made form with post method to other file having meta refresh and echoed url in meta refresh value! Thats it! Meta refresh will not encode your url.

How to output valid html5 with codeigniter

i'm trying to properly validate external url from a codeigniter 3 view.
I have in my db an url like this :
http://www.example.com/content.php?id=test&article=3
and i want to make a link like this one ->
http://www.example.com/content.php?id=test&article=3
I tried this :
<?php
if (isset($c_url_redir)){
$c_url_redir = 'http://www.example.com/content.php?id=test&article=3';
echo anchor(htmlspecialchars($c_url_redir), "go", 'class="pure-button pure-button-primary" target="_blank"');
}
?>
but i have the same url as the first one.
Could you help me?
Try using url_encode() on just the get variables. Personally, I'd have the first part of the URL assigned to a variable then tack on the url_encoded get variables e.g.
$c_url_redir = 'http://www.example.com/content.php';
$get_variables = url_encode('id=test&article=3');
echo anchor($c_url_redir . '?' . $get_variables, "go", 'class="pure-button pure-button-primary" target="_blank"');

Redirecting to affiliate links based on words

I am working on PHP code for automatically redirecting a URL to an affiliate URL based on words in the URL.
This is the code I am using:
if (stripos($mylink, "amazon.in") > 0) {
if (strpos($mylink, "?") > 0) {
$tmp = $mylink."&affiliate_id";
} else {
$tmp = $mylink."?affiliate_id";
}
$loc = "Location:".$tmp;
header($loc);
exit();
}
But the problem is that when the Amazon URL is like: http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?ie=UTF8&node=4068584031 it adds the affiliate id like this: ie=UTF8&affiliate_id (it automatically removes the node and its value) but I want it to be like: ie=UTF8&node=4068584031&affiliate_id.
This means that I need to add &affiliate_id at the end of the URL every time if it is an Amazon link.
Actually I just want to achieve the following simple thing: if the URL doesn't have any ?s, I need to place ?affiliateid and if the URL has a ?, I just need to append &affiliateid at the end.
Can you suggest the very simple method for implementing what I require?
Let PHP do the string parsing for you. Let's say your affiliate id is XYWZ.
if (strpos($mylink, "amazon.in") !== 0) {
$urlarray=parse_url($mylink);
$urlarray['query']='affiliate_id=XYWZ&'.$urlarray['query'];
$newurl= $urlarray['scheme'].'://'.$urlarray['host'].$urlarray['path'].'?'.$urlarray['query'];
echo $newurl;
}
will output
http://www.amazon.in/English-Movies-TV-Shows/b/ref=sa_menu_movies_all_hindi?affiliate_id=XYWZ&ie=UTF8&node=4068584031
see? I didn't need to worry if there's a question mark. It will be stripped in the output of parse_url;
Edit: using $mylink instead of $theurl

HREF to call a PHP function and pass a variable?

Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

Categories