transfer variable betweet two php page - php

i have 3 page the first is html where i use then i send the data to the second page (php) with post where i have some condition, then i will send the data again for the third php page where i will put it in table (html)
my probleme is the data don't send to the third php page
the 2nd page
if(isset($_POST['matier']) and isset($_POST['semaine']))
{
$matier = $_POST['matier'] ;
$semain = $_POST['semaine'] ;
header('Location:EmploiMetierSemaine.php?'.$matier.' & '.$semaine);
}
the third page
<?php
$matier = $_GET['m'] ;
$semaine = $_GET['s'] ;
?>
any help please

You are not passing the params as parameters again but only values here:
header('Location:EmploiMetierSemaine.php?'.$matier.' & '.$semaine);
If you want to pass them as m and s you have to tell your location header so:
header('Location:EmploiMetierSemaine.php?m='.$matier.'&s='.$semaine);
Note that I also removed the spaces around the & since they don't make any sense in the URL.

in second page url should be like this EmploiMetierSemaine.php?m=matier&s=semaine
if(isset($_POST['matier']) and isset($_POST['semaine']))
{
$matier = $_POST['matier'] ;
$semaine = $_POST['semaine'] ;
header('Location:EmploiMetierSemaine.php?m='.$matier.'&s='.$semaine);
}

Nowdays If in same case you need to pass a values to multiple pages consecutive with redirection, maybe it's better to review and redesign your program. in other word it's like you are doing something in wrong way.
However, your 2th page must be somthing like this:
$matier = $_POST['matier'] ?? NUll;
$semain = $_POST['semaine'] ?? NUll;
header('Location:EmploiMetierSemaine.php?m='.$matier.'&s'.$semaine);
And use $_GET on 3th page.

Related

random(non- repeating) file generator on click of button in php

i want to display 5 php files content randomly but non repeating when clicked a button [NEXT] using php/javascript (that works in php desktop as well).
the code i used did displayed random web page on page load but i did came across repeated web page
this is the code i used for index.php file:
<?php
$RandomList = array();
$RandomList[] = "/review/review-a1.php";
$RandomList[] = "/review/review-a2.php";
$RandomList[] = "/review/review-a3.php";
$RandomList[] = "/review/review-a4.php";
readfile($_SERVER['DOCUMENT_ROOT'].$RandomList[rand(0,count($RandomList)-1)]);
?>
please suggest how to get non repeated files .
Just save paths you already used in the session:
//Read visited paths from session or create a new list.
//?? works only in PHP7+. Use isset()?: instead of ?? for previous versions
$visitedPaths = $_SESSION['visitedPaths'] ?? [];
//Your list, just slightly changed syntax. Same thing
$randomList = [
"/review/review-a1.php",
"/review/review-a2.php",
"/review/review-a3.php",
"/review/review-a4.php"
];
//Remove all paths that were already visited from the randomList
$randomList = array_diff($randomList, $visitedPaths);
//You need to check now if there are paths left
if (!empty($randomList)) {
//The user did not load all files, so we can show him the next one
//Use array_rand() rather than $array[rand(0, count($array) -1)]
$randomPath = $randomList[array_rand($randomList)];
readfile($_SERVER['DOCUMENT_ROOT'] . $randomPath);
//Now we need to save, that the user loaded this file
$visitedPaths[] = $randomPath;
//And we need to save the new list in the session
$_SESSION['visitedPaths'] = $visitedPaths;
} else {
//TODO: Add some logic in case all paths have been visited
}

Make a $_GET into variable or place into URL

How do you place a $_GET['****']; into a string or make it into a variable.
For Example i have this url:
http://localhost/PhpProject2/product_page.php?rest_id=3/area=Enfield.
I want to get the area and rest_id from the url. in order to redirect another page to this exact page.
echo"<script>window.open('product_page.php?rest_id= 'put get here'/area='put get here'','_self')</script>";
I have so far done this:
if(isset($_GET['rest_id'])){
if(isset($_GET['rest_city'])){
$_GET['rest_id'] = $rest_id;
}
}
This obviously does not work, so my question is how do i make the 2 $_GET into a variable or call the $_GET into the re-direct string.
What i have tired so far
echo"<script>window.open('product_page.php?rest_id=' . $GET['rest_id'] . '/area='put get here'','_self')</script>";
How or what is the best practice?
ok, first things first. in your URL you have to separate the parameters using an ampersand "&", like this
http://localhost/PhpProject2/product_page.php?rest_id=3&area=Enfield
Also, you have to assign the $_GET value to a variable, not the other way around, like this
$rest_id = $_GET['rest_id'];
so if you create a PHP file named product_page.php and use the url i gave you, and your PHP code looks like this, it should work..
<?php
if (isset($_GET['rest_id'])){
$rest_id = $_GET['rest_id'];
}
if (isset($_GET['rest_id'])){
$area = $_GET['area'];
}
$url = 'other_page.php?rest_id=' . $rest_id . '&area=' . $area;
header("Location: $url");
?>
The question here is why do you want to redirect from this page to the other, and not send the parameters directly to the "other_page.php"????

Grab number from URL and use it as variable in PHP

I'm working from a Wordpress website, and I need to be able to create a template for a real quick-and-simple custom order page.
The plan is to create pages so that the URL of said page will be http://www.website.com/order-1234
And then, the template being used for that page will have PHP in it that will try and grab the "1234" portion of the URL, and use it as a variable.
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$order_id = intval(substr($url, strrpos($url, '/order-') + 4));
echo $order_id;
But the above code is returning a zero "0"
try by using
www.website.com/order?id=1234
and use
$getid = $_GET['id']
also when you want to use the get to show the page use the request function
if ($_REQUEST['id'] == $getid) {
// do something here
}
something like that :)

Remove querystring value on page refresh

I am redirecting to a different page with Querystring, say
header('location:abc.php?var=1');
I am able to display a message on the redirected page with the help of querystring value by using the following code, say
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
echo 'Done';
}
}
But my problem is that the message keeps on displaying even on refreshing the page. Thus I want that the message should get removed on page refresh i.e. the value or the querystring should not exist in the url on refresh.
Thanks in advance.
You cannot "remove a query parameter on refresh". "Refresh" means the browser requests the same URL again, there's no specific event that is triggered on a refresh that would let you distinguish it from a regular page request.
Therefore, the only option to get rid of the query parameter is to redirect to a different URL after the message has been displayed. Say, using Javascript you redirect to a different page after 10 seconds or so. This significantly changes the user experience though and doesn't really solve the problem.
Option two is to save the message in a server-side session and display it once. E.g., something like:
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
This can cause confusion with parallel requests though, but is mostly negligible.
Option three would be a combination of both: you save the message in the session with some unique token, then pass that token in the URL, then display the message once. E.g.:
if (isset($_GET['message'], $_SESSION['messages'][$_GET['message']])) {
echo $_SESSION['messages'][$_GET['message']];
unset($_SESSION['messages'][$_GET['message']]);
}
Better use a session instead
Assign the value to a session var
$_SESSION['whatever'] = 1;
On the next page, use it and later unset it
if(isset($_SESSION['whatever']) && $_SESSION['whatever'] == 1) {
//Do whatever you want to do here
unset($_SESSION['whatever']); //And at the end you can unset the var
}
This will be a safer alternative as it will save you from sanitizing the get value and also the value will be hidden from the users
There's an elegant JavaScript solution. If the browser supports history.replaceState (http://caniuse.com/#feat=history) you can simply call window.history.replaceState(Object, Title, URL) and replace the current entry in the browser history with a clean URL. The querystring will no longer be used on either refresh or back/previous buttons.
When the message prompt ask for a non exsisting session. If false, show the message, if true, do nothing. session_start(); is only needed, if there is no one startet before.
session_start();
if ($_GET['var']==1 && !isset($_SESSION['message_shown']))
{
$_SESSION['message_shown'] = 1;
echo 'Done';
}
Try this way [Using Sessions]
<?php
//abc.php
session_start();
if (isset ($_GET['var']))
{
if ($_GET['var']==1)
{
if(isset($_SESSION['views']))
{
//$_SESSION['views']=1;
}
else
{
echo 'Done';
$_SESSION['views']=1;
}
}
}
?>
Think the question mean something like this?
$uri_req = trim($_SERVER['REQUEST_URI']);
if(!empty($_SERVER['REQUEST_URI'])){
$new_uri_req = str_replace('?avar=1', '?', $uri_req);
$new_uri_req = str_replace('&avar=1', '', $new_uri_req);
$pos = strpos($new_uri_req, '?&');
if ($pos !== false) {
$new_uri_req = str_replace('?&', '?', $new_uri_req);
}
}
if( strrchr($new_uri_req, "?") == '?' ){
$new_uri_req = substr($new_uri_req, 0, -1);
}
echo $new_uri_req; exit;
You can use then the url to redirect without vars. You can also do the same in js.
str_replace() can pass array of values to be replaced. First two calls to str_replace() can be unified, and filled with as many vars you like that needs to be removed. Also note that with preg_replace() you can use regexp that can so manage any passed var which value may change. Cheers!

html & php question (get command)

Want to include another value in my urls
so far I have
# valid pages
$page = array('splash', 'portraits', 'weddings', 'studio', 'about', 'mobile', 'personal');
# validate GET, default to splash if not provided
$_GET['q'] = (isset($_GET['q'])) ? strtolower(trim($_GET['q'])) : 'splash';
if (!in_array($_GET['q'], $page))
$_GET['q'] = 'splash';
require_once "{$_GET['q']}.html";
but I want to include sections into my site so I need an extra value (ie. ?q=portraits?z=studio )
I assume I could probably just duplicate the $get command and replace q with z... but suppose I don't want the second value to be necessary (so the default pages will display without extra commands) and i want it to target a div or frame within the page? Not sure if I can even target divs.
Just use a default value:
if (isset($_GET['z'])) {
$z = $_GET['z'];
}
else {
$z = NULL; // or 'default' or whatever you want.
}
You can in short write the above in one line:
$z = isset($_GET['z']) ? $_GET['z'] : NULL;
I am not sure what you mean with "target a div", but if you mean an anchor, you can do that via identifiers (id).
Call your PHP script via index.php?q=splash#text and in your splash.html file you somewhere have <div id="splash">. The browser should scroll there.

Categories