PHP Global Variable - Keep assigned value - php

I have the following declaration at the beginning of my PHP script:
$GLOBALS['monthselect'] = date('m');
$GLOBALS['yearselect'] = date('Y');
during the script I assign the following values:
$GLOBALS['monthselect'] = $_GET['mo'];
$GLOBALS['yearselect'] = $_GET['yr'];
Next, after a form submits, I want to redirect it to the same selection in GET. (This is all in the same PHP script)
header('Location: ?yr='. $GLOBALS['yearselect'] .'&mo=' . $GLOBALS['monthselect']);
Problem is, this always reloads the page with the date of today. Never the newly stored values. So always this output:
website.com/?yr=2013&mo=06
What am I missing here?

Could you please provide the places where do you set $GLOBALS['monthselect'] = $_GET['mo']; and $GLOBALS['yearselect'] = $_GET['yr']; Make sure they get executed..

Related

transfer variable betweet two php page

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.

If isnt set open txt name else

Essentially what I'm trying to achieve is, if the user hasn't selected one of the dropdown options, then a default .txt is opened, read and displayed. When the user does decide on a drop down option, it echos the users option. It works by getting the value of the option and setting that to a variable to set the open path.
Instead of opening my default which is 'example' when the user hasnt selected, it seems to pick one of the values at random and select it.
If the user hasnt selected an option, the default file path is p-changelog/example.txt
If the user has chosen and option, the file path becomes p-changelog/$userOp.txt (UserOp being the users option from the drop down).
if(!isset($_POST['userDateOp'])) {
$userOp = "example";
}else{
$userOp = $_POST['userDateOp'];
}
if(!isset($_POST['submit'])) {
$changelog = fopen("p-changelog/$userOp.txt", 'r');
$pageTxt = fread($changelog, 25000);
echo nl2br($pageTxt);
}
Entire Page: Code
I am new to PHP before criticising anything I've done.
userDateOp is always going to be set, as its posted every time.
What you are looking for is its value being empty
That function is
if(empty($_POST['userDateOp'])) {
You don't need to check if submit is set because you've already validated the existence of userDateOp. So this will only lead to an undefined variable notice for $changelog for default.
Your simplified script should be
if(!isset($_POST['userDateOp'])) {
$userOp = "example";
}else{
$userOp = $_POST['userDateOp'];
}
$changelog = fopen("p-changelog/$userOp.txt", 'r');
$pageTxt = fread($changelog, 25000);
echo nl2br($pageTxt);

Check for URL variable in PHP

I have a PHP page that takes a date variable: mypage.php?date=2015-07-14 and displays output.
How do I check if the date= is empty and if it is then insert and refresh the page with the current date?
I've tried to formulate a small script at the top of the page using _GET with the date value but am not sure how to handle the reload of the page?
You can simply check your input is not empty using empty function, and create a date (it will default to current, unless you pass parameter), and passed it as get parameter in header function to the same page.
if(empty($_GET['date']))
header('location:'.$_SERVER['PHP_SELF'].'?date='.date('Y-m-d'));
You can simply use the empty language construct from PHP to check if a date was supplied: http://php.net/empty
Then you could use the header-function to redirect if no date was entered in the url: http://php.net/manual/en/function.header.php
For example:
<?php
if (empty($_GET['date'])) {
header('location: ' . $_SERVER['REQUEST_URI'] . '?date=' . date('Y-m-d'));
}
Also note that any headers should be sent before generating any output in your script.
What you seek is a combination of isset and empty functions with redirect using header. The end result could look something like this
<?php
$rawDate = isset($_GET['date']) && !empty($_GET['date']) ? $_GET['date'] : false;
$requestDateTime = $rawDate ? DateTime::createFromFormat('Y-m-d', $rawDate) : false;
$currentDateTime = new \DateTime();
// If datetime from GET is invalid or is not current date...
if (!$requestDateTime || $currentDateTime->diff($requestDateTime)->days > 0) {
// Redirect to current page with current date
header(sprintf('Location: ?date=%s', $currentDateTime->format('Y-m-d')));
}

Simple PHP url Help needed

I am new to php, I need small help
I m creating a page in php named index.php
i want when someone view that page automatically a number or anything would be added to the url end
Like www.abc.com/index.php ---> www.abc.com/index.php?abc or ?132
when ever that index page is refreshed it should get a number or any variable in the end
Try this:
<?php
if (!isset($_GET["123"])) {
header("Location: " . $_SERVER["PHP_SELF"] ."?123");
}
$QS = $_SERVER["QUERY_STRING"];
$URL = "http://www.example.com/index.php";
// Check if Anything already assigned
if( empty($QS) ) {
// Generate Any RANDOM Number Here
$NUM = mt_rand(999, 9999);
// Reload Page and assign number
header("Location: {$URL}?{$NUM}");
}
Place this code in the very top of your page
Give this a go
<?php
$num = '123';
if(!isset($_GET[$num])){
header("Location: /path/to/page?$num");
}

Php: Session not working correctly

i have code:
$search = $_REQUEST['search'];
if(!isset($_REQUEST['search'])){
if(is_null($_SESSION['ss_search']))
$search = 0;
else
$search = $_SESSION['ss_search'];
}
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
firstly I enter number 1 and submit, browser return 1. Then I enter number 2 and submit, browser return 2 but I refresh browser, it return 1. I don't know why session stored value 2 but when refresh it return old value. i used session_start() on top.
Please try this code
$search = $_REQUEST['search'];
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
Try this and let me know.
I am not sure but try to remove (NOT)! from the if statement. It worked for me.
change
if(!isset($_REQUEST['search'])){
to
if(isset($_REQUEST['search'])){
Please let me know if you face any problem.
I have tested your code and amend it as following:
$search ="";
if(!isset($_REQUEST['search'])){
if(is_null($_SESSION['ss_search']))
$search = 0;
else
$search = $_SESSION['ss_search'];
}else{
$search=$_REQUEST['search'];
}
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
using $_REQUEST['search'] without checking if it is set, cause it to generate undefined index notice. so now $_REQUEST['search'] is called only when it is set, Another cause is if you have set the default value on your form input field equal to 1, and when you refresh the page and allow it to resend the form, it may send the default value of your form.

Categories