PHP GET variable with dash - php

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

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

Show HTML if URL contains a defined domain name, else show something else

I am building an admin panel that can be used on different sites. I need to show some specific HTML if the url contains a specific domain, and something else if not.
So, if the URL is something like this http:// (something) .mydomain.com/admin.... I would like to show some HTML.
If the URL is something like this http:// anotherdomain.com/admin... I would like to show something else.
Í have tried with this, but somehow didn't get that to work. What am I doing wrong?
Edit: Wrong code. See code below...
<?php
if (substr($_SERVER['REQUEST_URI'], 0, 5) !== '/mydomain.com') {
echo '<div id="stuff"></div>';
}
?>
Sorry: Posted the wrong php code. A bit too tired I think...
This is the one I messed with:
<?php
if (substr($_SERVER['SERVER_NAME']) !== 'mydomain.com') {
echo '<div id="stuff">test</div>';
}
?>
Please try below code
<?php
$url_segments = parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
if (strpos($url_segments['host'],'mydomain.com') !== false) {
echo 'true';
}else{
echo 'false';
}
?>
Thanks,
Try with that code:
if (preg_match ( string $pattern , string $subject)){}
http://php.net/manual/de/function.preg-match.php
http://php.net/substr
Your current substr is saying "If the first 5 characters of the request URI aren't /mydomain.com, print the stuff below."
You could try to do this correctly in a few ways. You could use strpos and checking to see if mydomain.com appears in the request URI, or a more complicated regex to find something like (mydomain.)
Change $_SERVER['REQUEST_URI'] for $_SERVER['SERVER_NAME'] if mydomain.com it's your domain name.
Also, the substr is returning 5 chars and you are comparing against 13 chars

Get content after question mark in 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

Passing unknown link variables to a new page

I have a link, an offer page and a destination page. I need to carry the variables from the original link and input them into the links on the offer page.
original link
www.example.com/offerpage.php?offer=1&aff_id=var1&aff_sub=var2
Where you see var1 and var2, those could be any number.
I'm assuming I could do something like this (this is a total guess, just want to make sure I do it correctly).
<?php
if(array_key_exists('aff_id', $_GET)){
$aff_id = $_GET;
}
else {
$aff_id = '1';
}
?>
Then the links on the offer page would be
www.offer.com/index.php?offer=1&aff_id=<?php echo $aff_id; ?>&aff_sub=<?php echo $aff_sub; ?>
and whats the correct format for doing multiples?
This should probably do what you want:
if (!array_key_exists('aff_id', $_GET)) {
$_GET['aff_id'] = 1;
}
echo http_build_query($_GET);
If the query string is offerpage.php?offer=1&aff_id=var1&aff_sub=var2then the output will be:
offer=1&aff_id=var1&aff_sub=var2
And, if the query string doesn't contain aff_id, i.e. offerpage.php?offer=1&aff_sub=var2 then the output will be:
offer=1&aff_sub=var2&aff_id=1

Need help deciphering PHP code

I am running through a jQuery Ajax tutorial here:
http://www.charlieperrins.com/2011/03/ajax-jquery-101/
Everything works perfectly but I have a question about this piece of code:
<?php if ($_POST['user']) : ?>
<?php
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
?>
I am most concerned with the very first line. What does that line do? I am trying to keep all the code in 1 set of php tags but I don't know how to do that. If I knew what the first line does, I might be able to figure it out. Any help is appreciated. I am trying to reverse engineer this to fit it into my app but can't do it without knowing what that top line does.
Thanks.
All this does is continues the if block until endif.
There is no endif, so nothing in this script runs unless there is data in $_POST['user'] that doesn't evaluate to false.
I would write this a bit differently:
<?php
if (isset($_POST['user'])) {
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
}
?>
The first line tests if the $_POST array has a key user, and that key contains a "truthy" (non-empty, among other things) value, indicating that a form was posted to this script. If no form data was posted, the rest of the script won't execute, such as if someone browsed directly to this PHP script without using the expected form to post to it. It is a technique often used when a form posts back to the same PHP script. Upon first arriving at the script, the $_POST will be empty. When the form is posted back to the same script, different actions can be taken when it contains values.
There need only be one <?php tag:
<?php
if ($_POST['user']) {
$user_id = $_POST['user'];
if (isset($db_data[$user_id])) {
$data = $db_data[$user_id];
} else {
echo 'Sorry, no user data matched your request - please try again';
die;
}
}
?>
This is Alternative syntax for control structures
<?php if ($_POST['user']) : ?> means if $_POST['user'] evaluates to true, execute the following code.
It can be compressed down to this:
<?php if ($_POST['user']) :
$user_id = $_POST['user'];
....
Also,
if ($_POST['user']) :
should be
if (isset($_POST['user']) && !empty(trim($_POST['user']))) :
That makes sure that $_POST['user'] has been set (generally $_POST contains variables from a form), and that it is not empty even with white-space removed.
See
Alternative syntax for control structures
$_POST
empty
trim
The if ($_POST['user']) line is saying this:
If the variable $_POST['user'] exists and is set to a non-false value.
The above condition fails if $_POST['user'] is 0, false, or '' (empty string).
It also isn't safely checking that value.
You are better off using:
if (isset($_POST['user'])) && $_POST['user'] != '')
This way no warning is output when PHP has display_errors and notices turned on.

Categories