How to get some portion in the url? - php

I am working in php. I want to get some portion of the url using php,
For Example, my url is "http://localhost:82/index.php?route=product/product&path=117&product_id=2153". i want route=product/product only.

Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:
<?php
if (isset($_GET['route'])) {
$route = $_GET['route'];
}else{
// Fallback behaviour goes here
}
Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filter extension:
<?php
echo filter_input(INPUT_GET, 'route');
You can read it using $_REQUEST as below:
<?php
echo $_REQUEST['route'];
?>

It sounds like simply $_GET['route'] will work, although that will only give you product/product. You can just fill in the rest yourself if you know the name of the parameter.

Those URL parameters are called get variables. You can retrieve them using the super global $_GET like so
$route = $_GET['route'];

try this,
<?php
$ans=$_GET['route'];
echo $ans;
?>

Using the following code,
<?php
if(isset($_REQUEST["route"]))
echo $_REQUEST['route'];
?>

Related

Using URL as a condition on php

I want to show on my site an element depending on my site's url.
Currently i have the following code:
<?php
if(URL matches)
{
echo $something;
}
else
{
echo $otherthing;
}
?>
I wanted to know how do I get the URL on the if condition, because I need to have only one php archive to show on many diferent pages
EDIT: The solution provided by Rixhers Ajazi doesnt work for me, when i use ur code i get the same URI for both of my pages, so the if sentence always goes by the else side, is any way to get the exact string u can see on the browser to the PHP code
http://img339.imageshack.us/img339/5774/sinttulocbe.png
This is the place where it changes but, the URL i get on both sides is equal, im a little bit confused
To get the URL, use:
$url = http://$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Use following syntax with URL
http://mysite.com/index.php?var1=val&var2=val
Now you can get the values of variables in your $_GET variable and use in if condition like
if($_GET['var1'])
You can do so by using the $_SERVER method like so :
$url = $_SERVER['PHP_SELF']; or $url = $_SERVER['SERVER_NAME'];
Read up on this more here
if($url == 'WHATEVER')
{
echo $something;
}
else
{
echo $otherthing;
}
?>
You can use different variables, e.g., $_SERVER["PHP_SELF"], or $_SERVER["REQUEST_URI"]. The first one contains the path after the server name and until a possible ? in the URL (the part with the GET parameters is excluded). The second one contains also the GET parameters. You can also retrieve the hostname used to connect to the server (in case you have a virtual host situation) using $_SERVER["HTTP_HOST"]. Therefore by concatenating all these you can reconstruct the full URL (if you really need it, maybe the script name is enough).

PHP echo only if another PHP reference exists?

Not sure if this is possible but what I am wanting to do is echo a html text link only if the page contains certain php code, in particular just the opening reference of 'myApi'. So if the page contains the following code:
<?php myAPI(variables); ?>
Then I would like to include this somewhere else on the page
<?php echo 'Click Here'; ?>
Any help would be much appreciated, thanks :)
Set an if somewhere, and use isset to test if your API has set a specific variable or not.
if(isset($apiVariable)) {
echo "link content...";
}
(The variable should be specific to your API, obviously.)
Update
I said "somewhere", but it's probably best to do the test logic it in a separate place than where you output the HTML. In that case, you'd need a flag for when you're ready to spit out the HTML:
isset($apiVariable) ? $apiFlag = true : $apiFlag = false;
// continue other PHP operations...
//...now your logic is finished, output HTML.
if($apiFlag) { echo "<a>Link</a>"; }
That way, when you re-visit the page later for revision or update, your logic not all mixed up with your display.
<?php if (isset($variable)): ?>
Click Here
<?php endif; ?>
The function isset() check if the variable is set and returns a boolean. so you can use it to check if a variable exist.

GET URL parameter in PHP

I'm trying to pass a URL as a url parameter in php but when I try to get this parameter I get nothing
I'm using the following url form:
http://localhost/dispatch.php?link=www.google.com
I'm trying to get it through:
$_GET['link'];
But nothing returned. What is the problem?
$_GET is not a function or language construct—it's just a variable (an array). Try:
<?php
echo $_GET['link'];
In particular, it's a superglobal: a built-in variable that's populated by PHP and is available in all scopes (you can use it from inside a function without the global keyword).
Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:
<?php
if (isset($_GET['link'])) {
echo $_GET['link'];
} else {
// Fallback behaviour goes here
}
Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filter extension:
<?php
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);
Last but not least, you can use the null coalescing operator (available since PHP/7.0) to handle missing parameters:
echo $_GET['link'] ?? 'Fallback value';
Please post your code,
<?php
echo $_GET['link'];
?>
or
<?php
echo $_REQUEST['link'];
?>
do work...
Use this:
$parameter = $_SERVER['QUERY_STRING'];
echo $parameter;
Or just use:
$parameter = $_GET['link'];
echo $parameter ;
To make sure you're always on the safe side, without getting all kinds of unwanted code insertion use FILTERS:
echo filter_input(INPUT_GET,"link",FILTER_SANITIZE_STRING);
More reading on php.net function filter_input, or check out the description of the different filters
The accepted answer is good. But if you have a scenario like this:
http://www.mydomain.me/index.php?state=California.php#Berkeley
You can treat the named anchor as a query string like this:
http://www.mydomain.me/index.php?state=California.php&city=Berkeley
Then, access it like this:
$Url = $_GET['state']."#".$_GET['city'];
I was getting nothing for any $_GET["..."] (e.g print_r($_GET) gave an empty array) yet $_SERVER['REQUEST_URI'] showed stuff should be there. In the end it turned out that I was only getting to the web page because my .htaccess was redirecting it there (my 404 handler was the same .php file, and I had made a typo in the browser when testing).
Simply changing the name meant the same php code worked once the 404 redirection wasn't kicking in!
So there are ways $_GET can return nothing even though the php code may be correct.
$Query_String = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1] );
var_dump($Query_String)
Array
(
[ 0] => link=www.google.com
)

PHP Require and Include GET

I would like to require a file but also pass GET variables through the url, but when I write:
<?php
require_once("myfile.php?name=savagewood");
?>
I get a fatal error. How would I accomplish this functionality in a different way, such that I don't get a fatal error?
variables will be available as normal you do not have to pass like this.
$name='savagewood';
require_once("myfile.php");
$name will be available in myfile.php
<?php
$getVarsArray = $_GET;
$postVarsArray = $_POST;
/* n number of variables and lines of code*/
include('script-a.php');
?>
Now in script-a.php has access to $getVarsArray and $postVarsArray and if in any case you are in doubt you can use $GLOBALS to access any variable throughout the life cycle of a script. But using global variables is a sin. :)
It is not necessary to pass the variables to the new file, because by including the new file the variables are maintained.
Remember that $ _GET is an array, and it can be modified within the script.
<?php
$_GET['name'] = "savagewood";
require_once("myfile.php");
?>
On this case, $_GET['name'] is accesible from the "myfile.php"
I think I have got a perfect solution to your problem. You can use implode function of PHP. But I would strongly recommend doing Shakti Singh's code.
SOLUTION CODE
echo implode(file('http://path-to-your-site/your-dir/myfile.php?name=savagewood'));

PHP include with HTTP get paramters

How would one go about using php include() with GET paramters on the end of the included path?
IE:
include("/home/site/public_html/script.php?id=5");
How would one go about using php include() with GET paramters on the end of the included path?
You could write into $_GET:
$_GET["id"] = 5; // Don't do this at home!
include(".....");
but that feels kludgy and wrong. If at all possible, make the included file accept normal variables:
$id = 5;
include("....."); // included file handles `$id`
You don't, include loads files via the local filesystem.
If you really wanted to, you could just do this, which would have the same result.
<?php
$_GET['id'] = 5;
include "/home/site/public_html/script.php";
?>
but then you might as well just define the variable and include it
<?php
$id = 5;
include "/home/site/public_html/script.php";
?>
and reference the variable as $id inside script.php.
Well you could use:
include("http://localhost/include/that/thing.php?id=554&y=16");
But that's very seldomly useful.
It might be possible to write a stream wrapper for that, so it becomes possible for local scripts too.
include("withvars:./include/that/thing.php?id=554");
I'm not aware if such a solution exists yet.

Categories