I would like to ensure that a $_GET variable is set to the value of another variable in my URL. If not then i would like to reload the page/url to show a set variable. This is the code im using:
session_start();
$openfile = $_SESSION['openfile'];
//check if $GET variable isset to the current $SESSION variable
if($_GET['file'] !== $_SESSION['openfile'] && !empty($_SESSION['openfile'])){
//if not then create new URL string
$hloc = basename($_SERVER['PHP_SELF']).'?file='.$openfile;
//change location to new URL string
header("Location: $hloc");
}
i dont understand why it doesn't work.
Any help would be great. Thanks.
edit: the problem is that the url doesn't reflect that of what the header info should be. this fixes itself fine if the page is then reloaded again. however, this is clearly not what I want.
if(isset($_GET['file'])) {
//its set means value is not equal to null or false
} else {
//its not set so redirect
}
http://php.net/manual/en/function.isset.php
Can you provide an example URL so we can see if that is coking it? Barring that, try this instead. Note how the $hloc is now outside the quotes:
session_start();
//check if $GET variable isset to the current $SESSION variable
if($_GET['file'] !== $_SESSION['openfile'] && !empty($_SESSION['openfile'])){
//if not then create new URL string
$hloc = basename($_SERVER['PHP_SELF']).'?file='.$openfile;
//change location to new URL string
header("Location: " . $hloc);
}
There is a mistake. Undefined variable: openfile.
Change this line
$hloc = basename($_SERVER['PHP_SELF']).'?file='.$openfile;
to
$hloc = basename($_SERVER['PHP_SELF']).'?file='.$_SESSION['openfile'];
As $openvariable is set(according to poster comment), so only fault is:
Either SESSION variable or GET variable is not set.Since the following code works perfectly.
<?php
session_start();
$_SESSION['openfile']='foo';
$_GET['file']='bar';
$openfile = $_SESSION['openfile'];
//check if $GET variable isset to the current $SESSION variable
if($_GET['file'] !== $_SESSION['openfile'] && !empty($_SESSION['openfile'])){
//if not then create new URL string
$hloc = basename($_SERVER['PHP_SELF']).'?file='.$openfile;
//change location to new URL string
header("Location: $hloc");
}
?>
As syntactically your code looks perfect,thus check properly whether Both GET Variable and Session Variable is set or not.
First Comment this statement header("Location: $hloc"); temporarily.
Put:
echo $_GET['file'];
echo $_SESSION['openfile'];
before if condition and check the output.It will help you to trace and know bug.
i found the problem.
it was due to having incorrect header information further down the script.
Related
I'm trying to setup a page that pulls just a part of the URL but I can't even get it to echo on my page.
Since I code in PHP, I prefer dynamic pages, so my urls usually have "index.php?page=whatever"
I need the "whatever" part only.
Can someone help me. This is what I have so far, but like I said, I can't even get it to echo.
$suburl = substr($_SERVER["HTTP_HOST"],strrpos($_SERVER["REQUEST_URI"],"/")+1);
and to echo it, I have this, of course:
echo "$suburl";
If you need to get the value of the page parameter, simply use the $_GET global variable to access the value.
$page = $_GET['page']; // output => whatever
your url is index.php?page=whatever and you want to get the whatever from it
if the part is after ? ( abc.com/xyz.php?......) , you can use $_GET['name']
for your url index.php?page=whatever
use :
$parameter= $_GET['page']; //( value of $parameter will be whatever )
for your url index.php?page=whatever&no=28
use :
$parameter1= $_GET['page']; //( value of $parameter1 will be whatever )
$parameter2= $_GET['no']; //( value of $parameter2 will be 28 )
please before using the parameters received by $_GET , please sanitize it, or you may find trouble of malicious script /code injection
for url : index.php?page=whatever&no=28
like :
if(preg_match("/^[0-9]*$/", $_GET['no'])) {
$parameter2= $_GET['no'];
}
it will check, if GET parameter no is a digit (contains 0 to 9 numbers only), then only save it in $parameter2 variable.
this is just an example, do your checking and validation as per your requirement.
You can use basic PHP function parse_url for example:
<?php
$url = 'http://site.my/index.php?page=whatever';
$query = parse_url($url, PHP_URL_QUERY);
var_dump(explode('=', $query));
Working code example here: PHPize.online
I use a variable, value of which is a random string. Every time I refresh the page, that string changes. All good but I need to echo also the random string that was also generated before the new refresh, in other words store that string into a permanent let's say variable. Example: 12345 is generated, reload, abcde is generated but I want 12345 to be echoed as well... How can I do this? Thanks
You can put that variable value in the php SESSION
<?php session_start();
$str = "";
if(!isset($_SESSION['var1']) || empty($_SESSION['var1']))
{
$str = ... // random a string
$_SESSION['var1'] = str; // save in session
}
else $str = $_SESSION['var1']; // get the value back from session
?>
Also on the logout time, instead of deleting the whole session global variable, you should delete the specific session.
session_unset($_SESSION['specific_session']);
Hi im making a CMS with responsive HTML5 front
this part of the code retrieves the relevant information from url so that the relevant information can be displayed
this is the notice i am getting
Notice: Undefined index: pid in /home/hj016/public_html/SSTW/index.php on line 7
require_once "script/connect_to_mysql.php";
// Determine which page ID to use in our query below
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']);
// filter everything but numbers for security
}
is there any way to hide this ??
try
if (!isset($_GET['pid'])) {
basically you are calling for an undefined index even though you are trying to check if its not defined. The safe way to do this is with isset as it wont error if its not defined, because your checking existence not value.
On a production server you would generally hide notices anyway. But checking using isset($_GET['pid']) would be the preferred approach.
require_once "script/connect_to_mysql.php";
// Determine which page ID to use in our query below
if (!isset($_GET['pid']) || empty($_GET['pid'])) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']);
// filter everything but numbers for security
}
hows that? will make sure its set and not set to just like pid=
How to pass an array from one file to another using include using PHP language?
I have one file with some language array(language/langen.php):
global $lang;
$lang['Here'] = 'Here';
$lang['Date'] = "Date";
In other file I have:
include base_url().'language/lang'.$_COOKIE['lang'].'.php';
var_dump($lang);
*(My mistake by coping code - true is var_dump($lang))*
But it shows me an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: lang
How to solve this problem and what did I do wrong?
First of all:
You should never use cookie value directly in include statement - its pretty easy to make fake cookie and mess up in your application.
I suppose you just don't have cookie with name lang or variable $lang has never been initialized before.
To check if cookie exists and is in correct format you can do like that:
// set default lang code
$langCode = 'en';
// check if cookie exists and if contains string build from 2 characters from range a-z
// check also if file exists
if (isset($_COOKIE['lang'] && preg_match('/^[a-z]{2}$/', $_COOKIE['lang']) &&
file_exists(base_url().'language/lang'.$_COOKIE['lang'].'.php')) {
$langCode = $_COOKIE['lang'];
}
include base_url().'language/lang'.$langCode.'.php';
in included file you should check if variable $lang exists
if (!isset($lang)) $lang = array();
$lang['Here'] = 'Here';
$lang['Date'] = "Date";
also I think using global here is pointless as from your example it looks like its the same scope.
anyway for me much cleaner solution would be:
// first_file.php
$langCode = 'en';
if (isset($_COOKIE['lang'] && preg_match('/^[a-z]{2}$/', $_COOKIE['lang']) &&
file_exists(base_url().'language/lang'.$_COOKIE['lang'].'.php')) {
$langCode = $_COOKIE['lang'];
}
$lang = include base_url().'language/lang'.$langCode.'.php';
// langen.php
return array(
'Date' => 'Date',
'Here' => 'Here',
);
EDIT
One more thing - If base_url() is returning web URL (like http://example.com...) then it is also wrong (and also can cause problem as langen.php will contain at least Notice message when included this way) - should be included with valid file path
Shouldn't it be?
include base_url().'language/lang'.$_COOKIE['lang']['Here'].'.php';
Or else it would just return array()
First of all, I don't see the point of var-dumping variable $data, if there is no such thing in code you posted. If you have a cookie called "lang" in your browser, then you should be fine. You could always check this by
var_dump($GLOBALS['lang']);
in your code. It should print the array of values from your lang*.php file.
In my code i am trying to store a variable between two pages but i either get a string return "images" or 0... or nothing- tried casting it.. echoing in on one page works and displays correct number but as soon as you click through the view to the next page- its lost- i tried turning on cs_sessions in the db and made no difference
<?php
public function carconfirm($car_id = '')
{
if(empty($car_id))
{
redirect('welcome');
}
$this->load->model('mcars');
$car = $this->mcars->getCar($car_id);
$data['car'] = $car->row_array();
$car_id = (int) $car_id;
$this->session->set_userdata('flappy', $car_id);
echo $car_id;
//insert details
//display details
$this->load->view('phps/carconfirm',$data);
}
function confirm()
{
//get vars
$time_slot = $this->session->userdata('slot');
$person_id = $this->session->userdata('person_id');
$car_id = $this->session->userdata('flappy');
$insert_array = array( 'slot'=> $time_slot ,
'car'=> $car_id,
'person_id'=> $person_id
);
print_r($insert_array);
$this->load->model('mbooking');
$result = $this->mbooking->addbooking($insert_array);
if($result)
{
redirect('welcome/options');
}
}
?>
the variable I'm losing is flappy- i changed the name to see if that was the problem
Finally, I fixed this. Using this answer in SO too : codeigniter setting session variable with a variable not working, I scan my js/css for missing resources. Turn out that, my thickbox.js refer to loadingAnimation.gif in, yes, images folder. That's where the images come. Having fix the missing file, the sesion variabel working just fine.
Not sure, why CI replace (maybe) last added session variabel using this, but maybe it's because CI is not using $_SESSION global variabel. CMIIW. I use this article as a hint : http://outraider.com/frameworks/why-your-session-variables-fail-in-codeigniter-how-to-fix-them/