Multiple GETs in URL at the same time? - php

I'm trying to GET variables out of an URL, but everytime the URL already has a query, it gets overwritten by the next query. Example:
I have a link;
Page 34
When you click on that link this link becomes visible;
Item 45
But when I click that link, the other one get overwritten so the URL looks like;
www.domainname.ext/?item45
But I want it to look like;
www.domainname.ext/?page=34&item45
Any way to do that?
Thanks in advance!

From within a given "page", you need to store the page ID and use that stored value when you create "item" links.
<?php
$pageID = $_GET['page'];
// ....
?>
Item 45

You can also youse http_build_query(); to append more params to your URL
$params = $_GET;
$params["item"] = 45;
$new_query_string = http_build_query($params);
PHP http_build_query
for instance:
$data = array('page'=> 34,
'item' => 45);
echo http_build_query($data); //page=34&item=45
or include amp
echo http_build_query($data, '', '&'); //page=34&&item=45

<a href="?page={$page->id}&item={$item->id}">
Page {$page->id}, Item {$item->id}
</a>

When outputting the links, you'll have to include all of the relevant query string parameters; there's no automatic "merge".
If you could change your server-side stuff to using RESTful URLs, you could get this sort of behavior. E.g., starting with
http://www.domainname.ext
this link
<a href='page34'>Page 34</a>
would take you to
http://www.domainname.ext/page34
after which this link
<a href='item43'>Item 43</a>
would take you to
http://www.domainname.ext/page34/item43
...but that requires quite a big change in your server-side stuff.

Related

How To Auto Detect Url Format ? And Auto Create The Right Secured Echo or Printf?

Trying in building a Searchengine.
Need to echo user submitted links on Searchengine Result Pages. Problem is, different submitted urls will be in different php format.
Q1. How to auto detect the format ?
Q2. My searchengine will be opening the iFrame (of user submitted links) to other users (keyword searchers). How will my php script automatically know which part of the url to run htmlentities() and which parts to run the urlencode() ?
I can't be manually check the url format on a million link each day that my users submit to me everyday.
I mean, if I was opening an iFrame to my own link then no problem as I know my own link's format:
Example:
$url = 'http://localhost/test/pagination.php';
$search = $domain;
$tbl = 'linking_histories';
$col= 'domain';
$i = 1;
$limit = 2;
printf(
"<iframe src='%s?mysql_tbl=%s&mysql_column=%s&keyword_search=%s&result_limit_per_page=%d&page_number=%d'></iframe><br>",
htmlentities($url),
urlencode($tbl),
urlencode($col),
urlencode($search),
$limit, // %d place-holder will force integer
$i,
urlencode($limit),
urlencode($i)
);
I mean, a user might submit a normal static link like so:
A.
'http://localhost/test/pagination.php';
Or, a dynamic one, like these:
B.
'http://localhost/test/pagination.php?keyword=cars'; //%s (printf).
C.
'http://localhost/test/pagination.php?page=4'; //%d (printf).
D.
'http://localhost/test/pagination.php?keyword=cars&page=4';
//%s (printf) & %d (printf).
For example A, this php code is ok to echo the url in the iFrame:
$url = 'http://localhost/test/pagination.php';
printf(
"<iframe src='%s'></iframe><br>",
htmlentities($url),
);
For the example B submitted link, this particular php code is fine to echo the url in the iFrame:
$url = 'http://localhost/test/pagination.php';
$search = $domain;
printf(
"<iframe src='%s?keyword_search=%s'></iframe><br>",
htmlentities($url),
urlencode($search),
);
For example C, this particular php code is correct to echo the url in the iFrame:
$url = 'http://localhost/test/pagination.php';
$i = 1;
printf(
"<iframe src='%s?page_number=%d'></iframe><br>",
htmlentities($url),
$i,
urlencode($i)
);
For D, this particular php code is correct:
$url = 'http://localhost/test/pagination.php';
$search = $domain;
$i = 1;
printf(
"<iframe src='%s?mysql_tbl=%s&mysql_column=%s&keyword_search=%s&page_number=%d'></iframe><br>",
htmlentities($url),
urlencode($search),
$i,
urlencode($i)
);
As you can see from the above, notall the 4 links on the 4 iframes are in same url format.
One uses just htmlentities and no urlencode,
another uses the htmentities plus one urlencode ONLY
while another uses the htmlentities and TWO urlencode
and so on.
Some links have INT while others don't.
Now since each user submitted link will be different to each other, then I can't use one set of printf to echo all url formats.
So how to detect the url format on auto to generate the right printf with the right data type on the printf (eg. '%s', '%d") for that particular url the user submits ?
Is there any function in php that can detect the url type to tell me which functions (htmlentities, urlencode(), %s, %d, etc.) to use on which part of the url ? You know the var_dump() tells you the data type. Something like that I am looking for.
Care to show an code example how to achieve my purpose ?
Remember, I need to secure the link outputs so nobody can inject any link in the iFrames ?
**EDIT:
Do I use htmentities() or urlencode() here ?
Or both ?
Imagine url is either this:
$url = 'http://localhost/test/pagination.php?tbl=links&col=domain&search=elvanja.com&page=1&limit=5';
Or, this:
$url = http://www.elvanja.com/contactus.php;
Example 1:
printf("<iframe src='%s'></iframe><br>",
htmlentities($url));
Example 2:
printf("<iframe src='%s'></iframe><br>",
urlencode($url));
Example 3:
printf("<iframe src='%s?tbl=%s&col=%s&search=%s&limit=%d&page=%d'></iframe><br>",
htmlentities($url),
urlencode($url));
I going for EXAMPLE 3, what you say ?**

Fetching specific URL with preg_match

This is my code :
$patt = "#href=\"(.*?)\"#";
preg_match($patt,$data,$match);
echo $match[1];`
i.e. theres a URL in the HTML code of the page $data
<a href="http://aba.ai/iEU9x">
I want to grab this link above. Thanks

PHP redirect but in parent frame

I have a small time url shortner at http://thetpg.tk using a simple php script and MySQL.
What it does is to get the id and matches it in the SQL Database and redirects it to the specified link found in the Database using header().
But if I have a frameset with source as something like http://thetpg.tk redirected link is loaded inside the frame instead of the parent window.
For e.g. look at the page source of
http://thetpgmusic.tk which has the frame source as
http://thetpg.tk/b which further redirects to
http://thepirategamer.tk/music.php .
I want (1) to load (3) as the parent, but just by making changes in the functions in (2) .
So is there a function like
header(Location:http://thepirategamer.tk/music.php, '_parent');
in php, or is there any other way to implement it?
NOTE: I can't change anything in (2).
Thanks in advance ! :)
There are tree solutions that can help you do this:
First solution:
This solution may involve php if you're using echo to generate your html code, when you need to output an a tag, you should make sure to add the atribute target='_parent'
<?php
echo ' Click here ';
?>
problem :
The problem with this solution, is that it doesn't work if you need to redirect in the parent window from a page that you don't own (inside the iframe). The second solution solves this problem
Second solution:
This second solution is totally client-side, wich means you need to use some javascript. you should define a javascript function that addes the target='_parent' in every a tag
function init ()
{
TagNames = document.getElementById('iframe').contentWindow.document.getElementsByTagName('a');
for( var x=0; x < TagNames.length; x++ )
TagNames[x].onclick = function()
{
this.setAttribute('target','_parent');
}
};
Now all you need to do is to call this function when the body is loaded like this
<body onload="init();"> ... </body>
problem:
The problem with this solution, is that if you have a link that contains an anchor like this href="#" it will change the parent window to the child window To solve this problem, you have to use the third solution
Third solution:
This solution is also client-side and you have to use javascript. It is like the second solution except that you have to test if the link is a url to an external page or to an anchor before you redirect. so you need to define a function that returns true if it's a link to an external page and false if it's a simple anchor, and then you'll have to use this function like this
function init ()
{
TagNames = document.getElementById('iframe').contentWindow.document.getElementsByTagName('a');
for( var x=0; x < TagNames.length; x++ )
TagNames[x].onclick = function()
{
if ( is_external_url( this.href ) )
document.location = this.href;
}
};
and you also need to call this function when the body is loaded
<body onload="init();"> ... </body>
don't forget to define is_external_url()
update :
Here is the solution to get the url of the last child, it's just a simple function that looks from frames and iframes inside the paages and get the urls
function get_last_url($url)
{
$code = file_get_contents($url);
$start = strpos($code, '<frameset');
$end = strpos($code, '</frameset>');
if($start===false||$end===false)
{
$start = strpos($code, '<iframe');
$end = strpos($code, '</iframe>');
if($start===false||$end===false)
return $url;
}
$sub = substr($code, $start,$end-$start);
$sub = substr($sub, strpos($sub,'src="')+5);
$url = explode('"', $sub)[0];
return get_last_child($url);
}
$url = get_last_url("http://thetpgmusic.tk/");
header('Location: ' . $url);
exit();

Copying a Word and saving it as a Variable

I am building an online spanish dictionary. I obtain the definitions legally from another site. For specific words the user may search for, a list of suggestions comes up.
If you click on any of those suggested words, error page pops up: http://verbum.xtrweb.com/verbumpost.php?word=boedo&word0=hola . How can I make those listed words run through the code pasted below: (1) retrieve definition (2) change style.
I know that a unique URL exists for every one of those suggested words (inspect element). If you paste together that code with the original site's URL parameters, you get what I need.:
"http://lema.rae.es/drae/srv/search?id=AVNYdnea1DXX2EH9E2mb"
up to "srv/" belongs to site
the rest is from the specific word (in this case, the first suggested, "bordar"
The Process:
<?php
$words = array('word','word0','word1','word2','word3','word4','word5','word6','word7','word7','word9',
'word10','word11','word12',
'word13','word14','word15');
function url_decode($string){
return urldecode(utf8_decode($string));
}
// we'll use this later on for loading the files.
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
// string to replace in the head.
$cssReplace = <<<EOT
<style type="text/css">
//blabla
</style>
</head>
EOT;
// string to remove in the document.
$spanRemove = '<span class="f"><b>.</b></span>';
$styleRemove =
// use for printing out the result ID.
$resultIndex = 0;
// loop through the words we defined above
// load the respective file, and print it out.
foreach($words as $word) {
// check if the request with
// the given word exists. If not,
// continue to the next word
if(!isset($_REQUEST[$word]))
continue;
// load the contents of the base url and requested word.
$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));
// replace the data defined above.
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = str_replace($spanRemove,"", $contents);
$data = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/\1', $data);
// print out the result with the result index.
// ++$resultIndex simply returns the value of
// $resultIndex after adding one to it.
echo "<div id='results' style='
//bla bla
}
?>
I don't think I understand your question, but I do see that you are calling preg_replace on an uninitialized variable - $data.
Maybe you want this instead?
$data = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/\1', $contents);
Like I said, I don't fully understand your question, but I do see that as a potential problem.
EDIT:
From your comment, it looks like you want to append the href of the link being clicked to a base url and request that page's data. Can you use jQuery? Like this:
var baseUrl = "http://lema.rae.es/drae/srv/"; // base url
//click function that binds to all anchors within a list item within a ul. in order
//to get more precise, you may need to add some classes or ids
$('ul li a').click(function(){
var src = $(this).prop('href'); //this grabs the href property of the anchor
$(this).find('span').css('propery', 'change to this'); //change css property of span child of this anchor
//or if you want to add a class with styles to the element
$(this).find('span').addClass('class name');
//ajax request to get page data
$.ajax({
type: 'POST',
url: baseUrl+src //appending the query to the base url
}).done(function(data){
//append the returned html to a div, or any element you desire
$('#myelement').append(data);
});
});
Hope this helps. If you need any help with jQuery just let me know.

How to know whether i have clicked the particular link in the next page

I have created certain links using a for loop in PHP.
foreach($storage as $val)
{
if($val!="")
{
echo "$val";
echo "Link";
}
}
Page will look like
content1 Link to b.html
content2 Link to b.html
content3 Link to b.html
content4 Link to b.html
...
...
In the next page I want to retrieve the correct content based on the link clicked.
Example: If I click the second link the content to be retrieved is "content2".
How can I accomplish this?
Create your links like that:
echo 'Link'
Then, on the other page, use this code:
$content = isset($_GET['content']) ? $_GET['content'] : '';
Then $content contains whatever was passed in the URL - or an empty string if the param was missing.
You could set Get parameters so that your links are to b.html?var=content2
Then you can just read the Get Parameters
echo $val ."Link";
and you can then view this value with
echo $_GET['var'];

Categories