How to make URL change content of a website? [duplicate] - php

This question already has answers here:
Correct syntax for hyperlink URL with PHP $_GET
(3 answers)
Closed 7 years ago.
Hope you'll be able to help me, as I searched all around Google with various keywords and couldn't find anything relevant to me, or I just don't know what this thing I'm curios about is called.
I have a website, which I want to be personalized, according to the URL string.
So I'd link to send my friend a link like http://example.com/index.php?name=Joanna and it would change the beggining of my page to "Hi Joanna!". I just want it to be simple as that and integrate it into PHP or HTML (which is easier), but don't know how to achieve that.
Any help would be appreciated :)
P.S. - Keep the "How you don't know that?" remarks away , I'm new at this and am still learning.

See HTTP GET Variables
If your URL is
www.example.com/index.php?name=Joanna
it means that $_GET["name"] is "Joanna".
So in PHP, you can output:
<?php echo "Hi ".$_GET["name"]."!"; ?>
and it will output, in this case:
Hi Joanna!
You can also check that the variable exists and isn't empty by doing one of the following:
isset($_GET["name"]) //will return true even if $_GET==""
!empty($_GET["name"])
$_GET["name"]!=""
So you can change your PHP output to:
if(!empty($_GET["name"])){
$name = $_GET["name"];
} else {
$name = "guest";
}
echo "Hi, ".$name."!";
which will output "guest" if $_GET["name"] is empty.
Shorter version of the above:
echo "Hi, ".(empty($_GET["name"]) ? "guest" : $_GET["name"])."!";

Related

The right way to do preg_match and preg_replace [duplicate]

This question already has answers here:
PHP: Best way to check if input is a valid number?
(6 answers)
Closed 2 years ago.
So I have been trying to bring back to life my very old website.
I started with replacing ereg with preg, but it's been a very long time since I have written any PHP.
At the moment I am stuck on this:
$_POST['amount'] = preg_replace("/[^0-9/]",'',$_POST['amount']);
$_POST['amount'] = round($_POST['amount']);
if (!preg_match('/[^0-9]/', $_POST['amount'])) {
echo "Invalid amount.";
}else {
echo "Passed";
}
I'm not entirely sure where I am going wrong. Should it be !preg_match or preg_match for example?
Edit:
$_POST['amount'] allows a user to enter a number and needs to replace anything else other than a number if attempted.
Not sure what the error is (i.e. what is the current output and what was expected). However, one mistake in the prompt is that the pattern for the first preg_replace appears to have a typo: /[^0-9/] should probably be /[^0-9]/
preg_replace("/[^0-9]/",'',$_POST['amount']);
The rest looks like correct syntax (i.e. preg_match returns 0 if there is no match).

Read Json with php, some troubles [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have tried, checked many previous topics, and i cant find the way.
Parsing JSON with PHP
There is the answer, but im blind; actually, im a medimu PHP programmer, but im new with Json.
Here is my Json:
{"result":"true","disponible":{"aaa":"0.00001362","bbb":"0.000392","ccc":"0.00788523","ddd":"0.00004443","eee":"0.0001755","fff":"0.1755","ggg":"797.64618376"}}
My code:
$balances = json_encode(get_balances(),true);
print_r($balances);
The screen show my Json, so everything is ok here. Now, i want take the bolded values from the json and assign it to PHP variables.
$variable1 = $balances["disponible"]["bbb"];
$variable2 = $balances["disponible"]["ggg"];
echo "Valor 1: ".$variable1 ."<br>";
echo "Valor 2: ";$variable2 ;
But it dont work. I tried with many combinantions and nothing.
What im doing wrong?
Thanks a lot in advance. Im blocked with this.
Replace: $balances = json_encode(get_balances(),true); with: $balances = json_decode(get_balances(),true); if you are trying to get associative array.

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'];
?>

Correct way to write this in PHP [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
I'm trying to link several variables from a database into html. All the data is stored in the DB, however I can't figure out how to link the variables through HTML. Below is my code that I have tried, but doesn't work properly.
echo 'test';
I know the code works properly if I just do something like this (it does return the name):
echo $row["name"];
So why doesn't it work properly with the + $row["name"] + in it? It works perfect as long as I don't try and add data with the +'s.
Thank you!
this is the correct way to write this
echo 'test';
Use a . in stead of +.
And on a sidenote, if the data in the db isn't yet escaped, also use htmlspecialchars() on $row["name"]
Try like this.
echo "<a href='".$row['name']."'>test</a>";

Building <a></a> when user enter an url [duplicate]

This question already has answers here:
Replace URLs in text with HTML links
(17 answers)
Closed 9 years ago.
I have a input field which user enters something. Basically when user enter an url which start with http:// https// or www. i want to store it like user's words...
I'm working with php and mysql. I think replace function is for it. But when I read about it, this function only working on all string as far as I know.
For example:
input:
Hi, how are you? Please check this link : http:// www.google.com and enjoy.
output should like below:
Hi, how are you? Please check this link : http://www.google.com and enjoy.
Try this: (I don't test this code)
$output = preg_replace("#(http://|www\.)(.*) #Uis",'$1$2', $input);

Categories