Get content after question mark in PHP - php

I am getting a request like this and the url looks like this : www.site.com/test.php?id=4566500
Now am trying to get the id number to make the code in test page work, is there a way to do this?
<?php
echo("$id"+500);
?>

You can access these values via the $_GET array:
<?php
echo($_GET['id'] + 500);
?>

This is basic PHP. You want to use the $_GET superglobal:
echo $_GET['id'] + 500;

Do not forget to check the right setting of your Getter parameter:
if (isset($_GET['id']) && preg_match("\d+", $_GET['id'])) {
// do something with $_GET['id']
} else {
// appropriate error handling
}
Remember that anyone can set the id parameter to any value (which can lead to possible XSS attacks).

You cannot access direct url parameter without using predefined PHP super global variable like $_GET["$parameter"] OR $_REQUEST["$parameter"].
So for : www.site.com/test.php?id=4566500
<?php
$id = (int)$_GET['id']; // Or $_REQUST['id'];
if(is_numeric($id)){
echo $id + 500;
}else{
echo $id;
}
?>
For more detail :
PHP $_GET Reference
PHP $_REQUEST Reference

Related

Get Parameter from URL using PHP

I have a url:
domain.com/?city=newyork&postback=test
I am currently successfully passing the postback parameter using the PHP below. However, I can not figure out how to also pass the city parameter.
<?php session_start();
if(isset($_SESSION['postback'])) {
if($_GET['postback'] == "") {
header ("Location: qualify-step2.php?postback=".$_SESSION['postback']);
}
}
?>
Can someone please help me edit the code successfully? Any suggestions are greatly appreciated.
You should use the $_GET superglobal to get params from url... In your case $_GET['city'] holds the value newyork and $_GET['postback'] holds the value test.
You mean like this
<?php session_start();
if(isset($_SESSION['postback'])) {
if($_GET['postback'] == "") {
header ("Location: qualify-step2.php?postback=".$_SESSION['postback']."&city=".$_SESSION['city']);
}
}
?>
For geting parameter from URL, you should use $_GET['param'];.
Note: In here, You should use Get method for sending data.

PHP GET variable with dash

The URL is something.php?id=123-10-1
If I echo it, it only prints out 123 but I need it to say 123-10-1.
I suppose the solution is very simple but I'm not seeing it.
Use php urlencode and urldecode functions.
Of course, You will have the full argument stored in GET array. You are not doing anything wrong. The mistake should be somewhere else. Try to use:
var_dump($_GET);
$equal = ('123-10-1' == $_GET['id']);
var_dump($equal);
To see what is wrong... Echo is not the best printing functino here, however, it also should print the full argument...
UPDATE - note
After we know what was wrong:
$id = isset($_GET['id']) ? (int)$_GET['id'] : '';
I would suggest something like that:
// Set part of the code
$id = isset($_GET['id']) ? $_GET['id'] : '';
// Verification part of the code
if (!is_numeric($id) && $id != '') {
throw new Exception('ID must be numeric.');
}
if ($id == '') {
// ID was not set in the url. Maybe there should be another action here?
}
IN that case, You are in full control of what is happening here. Modyfing GET values or POST values "on the fly" is not the good practice.
I hope it helps.
Try using an anchor tag like <a href="something.php?id=123-10-1"> and it will echo 123-10-1
You are searching for the function urlencode which encoding a string to be used in a query part of a URL.
This was my code.
$id = $_GET['id']
echo $id; //123
and it printed '123' only.
But
echo $_GET['id']; //123-10-1
printed it all. Seems weird stuff as it should work in the first place, but it works now.
Gotta check my php.ini to see what's this all about.
I have try your code it giving me correct response
-<?php echo $_GET['id']; ?>
Output 123-10-1

Get the id from the URL; PHP, MySQL

Hi i've been curious on how to get the id in the url like php?pid=4 and use it in an update statement in sql. Well heres my code but cant get it worked because of undefined variable id which the value is in the url.
my function.php
function update_spot(){
$id=$_GET[pid];
if (isset($_POST['update'])){
$sql="UPDATE reports SET date_time_started='$_POST[date1]' ,
date_time_finished= '$_POST[date2]',
barangay='$_POST[brgy]',
street= '$_POST[street]',
owner='$_POST[owner]',
cause='$_POST[cause]',
motive='$_POST[motive]',
firfighter='$_POST[firefighter]'.
civilian='$_POST[civilian]',
ifirefighter='$_POST[ifirefighter]',
icivilian='$_POST[icivilian]',
occupancy='$_POST[occupancy]',
ed='$_POST[ed]',
alarm='$_POST[alarm]'
where id='".$id."' ";
if (!mysql_query($sql)){ die('Error: ' . mysql_error()); } ?>
<script type='text/javascript'>alert('sucessful changed try it next time you log
in.');window.location='view_inbox.php';</script> <?php
}
}
it seems i cant get id in the url. my url show like this in the form php?pid=5
It should simply be
$id = $_GET['pid'];
Use Quotes on your $_GET array access
$pid = $_GET['pid'];
Also, you are mixing $_GET and $_POST. You should use one or the other, depending on your form's method (GET or POST).
You also want to change all the areas you access it. IE
barangay = '$_POST[brgy]';
This should be, and all other lines after it
barangay = $_POST['brgy'];
$pid = isset($_GET['pid']) ? 0 : intval($_GET['pid']) //to avoid problems in this case
#Up, in the example above it still should work, but will cause php warning saying that undefined php constant will be assumed as string.
I would dump get global and see if variable is there, eg var_dump($_GET);

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

PHP - Manually Set And Then Echo $_GET Data

How would I for example, take a url with some $_GET data, for example http://www.website.com/something?food=steak
How would I then output steak? My current situation is that I'm trying to use the Header function to redirect to a page where I have it so that if $_GET["duplicate"] is equal to 1, then echo this, else, echo nothing. But its not taking the $_GET data I can tell I did a var_dump($_GET);
<?PHP if ($_GET["duplicate"] == 1 )
{
echo "<h1>Username Taken!</h1>";
}
else
{
echo "";
}
?>
The above is using the url http://something.com/register?duplicate=1
It's just a variable, treat it like one:
echo $_GET['food'];
Everything after question mark is available in form of global array $_GET.
$a=$_GET["food"];
echo $a;
also
if url has ?food=steak&color=red;
$a=$_GET["food"];
$b=$_GET["color"];
more than one is possible. Also search for $_POST.
Alright, so I figured my issue out. I have a $_GET variable that gets the end of the page and declares it as "p" for page. I need to do the following to get it to work.
?p=createuser&duplicate=1

Categories