How to obtain part of the url with php [duplicate] - php

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 6 years ago.
I am coming up with a way to display user profiles. Right now I am able to insert the user_id into the url when going to a user's profile, but I am unsure of how to get the user_id from the url.
So at the end of the url I have this:
profile?user=41
I just want to be able to get the 41 (please note this number will change per profile) and then set it to a variable.
How would I do this?

<?php
echo $_GET['user'];
?>
all the parameters of the url are stored in the $_GET variable...

The parameter that are in your URL can be retrieved with the
predefined variable:
<?php
//$userId = $_GET["pramName"];
$userId = $_GET["user"];
// or
// using the $_REQUEST variable
?>

Related

How I redirect dynamic url with query string in php? [duplicate]

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 6 years ago.
I need to redirect dynamic URL like this "https://test.com?user=abc#gmail.com" to "https://test.com"
If I understand correctly, this URL is coming dynamically with a query string. And you want to remove the query string portion and redirect to this modified URL.
Let's assume the dynamic URL is stored in a variable $url.
Try this:
$url = "https://test.com?user=abc#gmail.com";
$modified_url = strstr($url, "?", true); // Remove the query string, which results in https://test.com
header("location:".$modified_url); // Redirect to the modified URL.
You can store the dynamic part in a variable and use the header() function to redirect your user.
$dynamicPart = "someguy#somedomain.com";
header("Location: https://test.com/index.php?username=".$dynamicPart);
exit;

GET a URL parameter with PHP [duplicate]

This question already has answers here:
GET URL parameter in PHP
(7 answers)
Get the full URL in PHP
(27 answers)
Closed 6 years ago.
I would like to capture the lang variable and then translate the page based on this variable.
global $jwp_lang;
$url = $_SERVER["REQUEST_URI"];
echo $url;
for example if the url contains http://localhost/about/?lang=fr I would like to capture this value.
You can easily capture the value of lang variable using php Super Global variable $_GET :
$lang = $_GET['lang'];
echo $lang;
It is better to pass the URL parameters using add_query_var, and get the parameter using get_query_var.
Because, they can handdle the set, and get of multiple parameters, and is the recommended way of getting URL passed as parameters.

send the variable to another page in php [duplicate]

This question already has answers here:
How to pass variables received in GET string through a php header redirect?
(8 answers)
Closed 7 years ago.
I have two pages in php ,one of them is exam page(exam.php) and another is result page(result.php), the result is calculated in exam page and must be sent to result page to display.(I don't have a form)
to send the result, Inside exam.php ,I write, header("location:result.php?result");
and to get the result inside result.php ,I write, $newresult=$_GET['result'];
but I receive error,and result didn't sent to the result page.
would you please guide me?
Using a URL to pass parameters can be done like so.
HTML
<a href='yourPage.php?name=Script47'>Send Variable</a>
PHP
<?php
if (isset($_GET['name') && !empty(trim($_GET['name'])) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES);
}
?>
Explanation
The HTML is fairly simple, we create a link which holds a parameter specified after the page extension (?name=[...]).
The PHP first checks if the name parameter which was passed isset to prevent an undefined index error, and we check if it isn't empty. The trim function removes white spaces so an string with a space isn't outputted (" "). When we know that the string has a value in it we sanitize it (never trust user input) and then we output it.
Reading Material
htmlspecialchars();
trim();
empty();
isset();
Try use session
exam.php
<?php
session_start();
$_SESSION['result'] = $result;
?>
result.php
<?php
session_start();
echo $_SESSION['result'];
?>

What to parameter value from returning url in php [duplicate]

This question already has answers here:
GET URL parameter in PHP
(7 answers)
Closed 7 years ago.
I have a client url[www.mysite.com/page.aspx]
when we put this url in browser then it revert the automatically
with this url
[www.mysite.com/page.aspx?|&CC=121&DD=123&AA=323|&CC=321&DD=555&AA=000]
Now we want to fetch that parameter value in php page through php or jquery.
Kindly Help and give the best suggestions.
You can simply use GET
$cc = $_GET['CC'];
$dd = $_GET['DD'];
You can access those values using $_REQUEST or $_GET

Can I get the value from URL without requesting it ? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I parse URL GET data having the URL stored in a variable?
Can I get the value from URL without requesting the page ( refresh ) or pressing on a link
I need the value of p from this url
http://palestinianz.com/?page=person&p=Betty-Shamieh-
to use it in the og:title meta tag of Facebook
$url = parse_url("http://palestinianz.com/?page=person&p=Betty-Shamieh-");
parse_str($url['query'], $data);
$p = $data['p'];

Categories