How to store php page name in variable? - php

I have a php page (running locally):
http://localhost/webpagefolder/home.php
When a different page is clickedon the URL changes to reflect it. So it now becomes:
http://localhost/webpagefolder/home.php?here=delivery
because I have selected the delivery page.
How would I store the part after the = in a variable?
Something like $name = delivery (or whatever is displayed after the =)
Thanks in advance

In PHP all the GET-variables are stored in the $_GET super global. In your example $_GET['here'] should contain 'delivery'.

Related

How to create a global variable and use it in other pages?

I tried this :
in page1.php :
$GLOBALS['nb_ajout_client'] = "rrrr";
in page2.php :
$GLOBALS['nb_ajout_client'] .= " kkkkk";
I displayed the value of $GLOBALS['nb_ajout_client'] but it is kkkkk.
So how to create a global variable and use it anywhere ?
Global variables only survive the lifetime of the program execution.
Each time you load a (PHP) page, you run a program from scratch. If you load a different page then you run a different program.
If you want to store data between them then you need to actually store it somewhere and then read it back from there in the other program.
If you want to do this globally, then the usual approach is to use a database.
If you want to do this on a per-user basis, then the usual approach is to use a session.
You could also pass the data via the browser (e.g. by putting it in the query string of a link and then reading it back from $_GET).
A variable is only 'global' in the current script. If you want to use a variable from page1, you need to include it from page2.
To pass variables to other page (other request) use PHP Sessions
you can try this.
g1.php
<?php
$GLOBALS['nb_ajout_client'] = "rrrr";
g2.php
<?php
include('g1.php');
$GLOBALS['nb_ajout_client'] .= " kkkkk";
var_dump($GLOBALS['nb_ajout_client']);
$GLOBALS are global in all scopes throughout a script.
To pass the value to another page:
use $_POST
use Session (server-side )
use cookies(client-side)

PHP: Store PHP variable in page to use in POST

Currently I put a constant on my webpage using PHP which I then send, with Ajax, to my POST function. However, this leaves it susceptible to hacking (the user could change the variable with Firebug), so is there a way to store the variable in the PHP of the page and then access it later on, in the POST method (or is the GET variable of the page still available in the POST function, since that's where I get the variable from)?
I think what you have wanted is to store the post value to use it later.
Here you would need to use $_SESSION
You can do it like
session_start();
// Save variables into session
$_SESSION['thevalue'] = $_POST['value'];
If you wish to store between successive calls from the same user use the follwing:
<?php
session_start();
$_SESSION["your variable/constant"] = yourvaule;
Now use the variable as needed, accessing it as $_SESSION["your variable/constant"]
Hope it helps, and it's what you're asking.

Hide PHP Variables in URL with url rewrite

I am working on a site that has to deal with reservation numbers and I need to pass variables to confirmation page. Currently when you arrive at confirmation.php the URL looks something like the below:
http://localhost/confirmation.php?reservation_id=1&proceed=no
These variables are sent via a link to this page from the index page. I need a way to either encrypt the $reservation_id OR rewrite my URL so it looks like this
http://localhost/confirmation.php
...but still have access to my variables. I have tried sessions and some encryption methods, but cant find anything that wont over complicate the page as I am trying to keep it as simple as possible. Forms with hidden fields is not an option, I am printing all the reservations in links with a loop from the database.
Thanks!
Use session variables. Create a random session variable name, and assign the reservation information to it:
foreach ($reservations as $r) {
$random = make_random_string(); // You need to write this function
$_SESSION[$random] = $r;
echo "<a href='reservation.php?id=$random'>...</a>";
}
Then reservation.php can look up $_SESSION[$_GET['id']] to get the reservation information.
It sounds like you need to start a session and pass the data in there. Or, you could POST the data instead (Use a form). But the session is probably the better choice.

Grab data from one PHP page and display it on another

I do not think that I am doing this the right way, but here goes it. I want to be able to have two php pages mypage.php and mypage2.php. mypage2.php has plain text data on it (say a 8 digit number) and it is just being displayed via the echo command.
I want to know if when I am on mypage.php if I can grab that number and display it on mypage.php rather than open up mypage2.php and display it there?
So a hypothetical example of mypage.php might be:
echo "My secret number is: " . magic_command("mypage2.php");
I know that one way to do this is with $_SESSION, but doesn't that involve still navigationg to mypage2.php, storing the number in $_SESSION. Then telling it to go back to mypage.php?
You can do:
include 'mypage2.php';
in mypage.php and in mypage2.php:
echo '12345678';
Easy as pie!
The way you're presenting your question makes me think that you're doing this the hard way. You could store the information in a variable, include the page, and echo it that way. However, I'm assuming there is more to it than that, in which case you're probably looking for a cURL, or even a file_get_contents solution. Now that I think about it, there are a few ways. Just have to do some research.
If fopen_wrappers are enabled, you can grab the output of one page into another with readfile(), like so:
$secret = readfile('http://server/blah/mypage2.php');
echo "My secret number is: $secret";
If that number is user-specific, you should probably store it in a database and then call it when needed.
Otherwise, if you absolutely want to store it in a separate php file, use include as yvesonline suggested. include acts as if the script in mypage2.php is now inserted into mypage.php.

Reading id from URL

My question is pertaining to extracting information from the URL using PHP.
I've three pages in the website I am building
catalogues.html
form_userdata.php
mail.php
The schema is as follows. The user will choose a file to download from catalogues.html
for eg
a href="form_userdata.php?id=1026&name=Vibrating Feeder - Heavy duty -VFH" target="_blank"
This id & name will be passed to form_userdata.php and after entering the details in form_userdata.php page, the page passes control to mail.php, that will check if the fields are all true and valid.
My question is how can I use the ID & name specified in "a href" in my code?
I am passing the ID and name from catalogues->form_userdata and collecting it in mail.php
Thank you for your valuable input
you can use sessions to store it when processing form_userdata.php and then use it in mail.php. That way you don't have to pass it in every URL.
when I add $id=$_POST['id']; at
form_userdata, I get an error saying
"undefined index: id"
That's quite logical, since you're passing them in the URI, and thus have to read them using $_GET in PHP, ie. do :
$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
I found this solution:
session_start();
$_SESSION['id'] = $id=$_GET['id'];
$_SESSION['name'] = $name=$_GET['name'];
this will pass the value of ID and Name to the next page.
Thank you for your help
you may use $_GET to collect and pass variables
You can access the named values in an URL (query string, query-info part) by using the $_GET superglobal. It will contain the value by it's name:
$_GET['id']
More information is in the the PHP manual: $_GET. That page is already pretty specific, I suggest you read this manual page as well: Variables From External Sources it more broadly describes how this works.
As input is very important in programming, it's really recommended to understand how it works so you can use the language for your needs more easily.

Categories