Show message with PHP when a parameter is passed in the URL - php

As new in PHP, I have a simple question. Am I able to do something like this?
E.g. the user, is redirected to a link like this:
http://website.com/directory?parameter
When that parameter is in the URL, I want a message to appear somewhere in the website, when the parameter is missing, just hide it.

get parameters are stored in the $_GET variable. So you can check if a get parameter is set with:
if(isset($_GET['parameter'])) {
echo 'parameter is set';
}
else {
echo 'parameter is not set';
}

When you pass a parameter like this:
http://site.com/page.php?this=that
You create what is called a GET request. In order to echo what's in the request, you'd do:
<?php echo $_GET['this']; ?>
In this instance, this would output:
that
To read more about PHP superglobals like $_GET, check out this link.

<?php
// Suppose this is URL http://site.com/page.php?url_var=val;
if(isset($_GET["url_var"]))
{
$msg = "your message";
}
else
{
$msg = "";
}
// NOW JUST PRINT $MSG WHERE EVER YOU WANT WITH OUT ANY CONDITION;
echo $msg;
?>

Related

How to redirect a page with variable value if condition is true in php?

how to jump a page if condition true and also i want to sent a variable value to secont page
if(isset($_POST['compair']))
{
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
$mail=$_SESSION['usermail'];(i want to sent "$mail" variable)
header("Location:resetpass.php?value = $mail ");
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}
please also tell me how to receive this variable on second page.
Your solution is correct. Just pay attention to spaces:
header("Location: resetpass.php?value=$mail");
exit; // as suggested by "nogad"
Also make sure that resetpass.php file is in the same directory of current page.
In resetpass.php you can get the variable by $_GET['value'] like:
<?php
if( isset($_GET['value']) ){
$mail = $_GET['value'];
}
After header("Location : resetpass.php?value=$mail");
exit(); // i.e quit the current page and go to resetpass.php
Then at resetpass.php collect $mail using the GET method.
if(isset[$_GET['value'])){
$the_mail = $_GET['value'];
}
I am writing from my handy. So apologies for any layout discripancies

how to bind url to variable in php

I want to bind the complete URL in a PHP variable.
My URL looks like this: http://develop.example.com/spf#users/admin.
To get the URL I use following:
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
and for the hash value is use this JS:
document.write(window.location.hash);
So my PHP variable looks like below:
$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"."<script>document.write(window.location.hash);</script>";
When I echo the $current_url I get this output: http://develop.example.com/spf#users/admin.
Now I want to have a check on the current URL:
if ($current_url != "http://develop.example.com/spf#users/admin") {
echo "you are NOT the admin";
}
else {
echo "you are the admin";
}
Unfortunately, even when the URL is exactly the same, he keeps hanging on: "you are NOT the admin".
What is going wrong here?
you can use following script and your code is correct but the problem is "document.write(window.location.hash);" this code remove it then run
<?php
$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$match="http://localhost:900/at/";
// echo base64_encode($current_url);
// echo "<br>";
// echo base64_encode($match) ;
if ($current_url==$match) {
echo "you are the admin";
}
else {
echo "you are NOT the admin";
}
?>

PHP Harmful URL protection

I've made this script, but the 4th line isn't right and I have really no clue how to solve this. I really appriciate if someone helps me. This is my code:
<?php
$url = $_GET["url"];
$badsite = array("http://check.com", "http://hotmail.com");
if($url == $badsite) {
echo "This URL is harmful.";
} else {
echo "Not harmful";
header("Location: " . $_GET["url"]);
}
?>
So the thing which doesn't work is the following line
if($url == $badsite) {
How can I make it so it checks if the GET contains a $badsite?
You don't want to check if the value equals the array, you want to check if it's in the array. Perhaps something like this:
if (in_array($url, $badsite)) {
// ...
}
Side note, you don't need (or want, really) this echo statement:
echo "Not harmful";
header("Location: " . $_GET["url"]);
You might get an error by emitting output before sending a header. But even if you buffer output or in some other way suppress that error, there's no reason to emit output when returning a redirect response. The browser would display it for only an instant, if at all. A redirect by itself is a complete HTTP response, no output is required.
In this case you can use the function in_array:
http://php.net/manual/en/function.in-array.php
<?php
$url = $_GET["url"];
$badsite = array("http://check.com", "http://hotmail.com");
if(in_array($url, $basite)) {
echo "This URL is harmful.";
} else {
echo "Not harmful";
header("Location: " . $_GET["url"]);
}
?>

pass query string get value into url link

I am trying to pass in a $_GET variable from a query string and pass it into a link to another page that has an application on it.
A customer will be directed to my page and the url will have the variable name merchantid. I need to take that on the home page, and pass it to the application page.
I've got it displaying on the home page as a test, so I know how to get it. I just need to know how to pass it the application page.
<?php
if (empty($_GET)) {
// no data passed by get
echo "<a href='{site_url}application'>Application</a>";
}
else
{
// The value of the variable name is found
echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
}
?>
My else link actually blows up currently.
Ok, here is my second try, with the same result. The link blows up when I pass in the merchantid into the url. Ex. www.mysite.com/?=merchantid=12345
<?php
if (empty($_GET)) {
// no data passed by get
echo "<a href='{site_url}application'>Application</a>";
}
else
{
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "DefaultMerchant";}
echo "<a href='{$site_url}application?merchantid=" .$merchantid ."'><Application </a>";
}
?>
Why your code is not working
You're not telling php that "merchantid" is a variable nor you're defining it.
Solution
Replace
echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
With
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "";}
echo "<a href='{$site_url}application?merchantid=" .$merchantid ."'><Application></a>";
}
Updated code
<?php
$site_url = 'http://'.$_SERVER['HTTP_HOST'].'/';
if (empty($_GET)) {
// no data passed by get
echo "<a href='{$site_url}application'>Application</a>";
}
else
{
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "DefaultMerchant";}
echo "<a href='{$site_url}application?merchantid=".$merchantid."'>Application</a>";
}
?>
$_GET is an array indexed by whatever values are in the query string. For example:
http://sit.url.com?merchantId=12&foo=bar
would place the following in the $_GET array:
$_GET['merchantId'] = "12"
$_GET['foo'] = "bar"
You will want a block in your code to initialize a $merchantId variable based on the presence of those values from $_GET:
//folks commonly use ternaries for this:
$merchantId = (isset($_GET['merchantId'])) ? $_GET['merchantId'] : false
Which is a shorthand way of stating:
if (isset($_GET['merhantId']) {
$merchantId = $_GET['merchantId']
} else {
$merchantId = false;
}
As Angelo and C.Coggins mentioned, don't forget the "$" in front of your variable in php.
You either need to assign $_GET['merchantid'] to $merchantid first, or replace $merchantid with $_GET['merchantid'] unless you have register_globals turned on, which you really shouldn't use.
So either add this:
$merchantid = $_GET['merchantid'];
or use this:
echo "<a href='{$site_url}application?merchantid=" . $_GET['merchantid'] . "'><Application></a>";
Besides that, as others pointed out, your original code is missing a $ before the variable name.

passing values through the URL when using the $_GET just get blank page

this is the code used to get the information from the database, the images I have taken off the echo statement for the time being, and just the name of the product. When I click on the product name it sends me to cart.php and should pass the value in the URL it shows in the browser when I hover over the text but when i click it send me to cart.php and just shows a blank page
$product_types = get_all_subjects2(); function is just the query
while($products = mysql_fetch_array($product_types))
{
$name = $products['name'];
$address = $products['image_location'];
echo '<ul>';
echo "<li><a href=\"http://localhost/project/cart.php?subj=" . urlencode($products["name"]) .
"\">{$products["name"]}</a></li>";
The code
<?php
if (isset($_POST['subj']))
{
$a = $_POST['subj'];
echo $a;
}
else {
echo"error";
}
?>
$_POST stored values following POST request. If you navigate via a plain link, you're doing GET requests. So look in $_GET.
Also it might be a good idea to always output valid HTML. Otherwise the browser might or might not render what you're outputing.
You send your arguments via GET. So asking for $_POST isn't the right idea ;).
For Debbugging it's nice to have this statement in your target site:
echo '<pre>', print_r($_REQUEST), '</pre>';
Best,
Christian
Addition: tested the code.. just works fine:
<?php
echo '<pre>', print_r($_REQUEST), '</pre>';
if (isset($_GET['subj'])) {
$a = $_GET['subj'];
echo $a;
} else {
echo 'error';
}
?>

Categories