On running the following code I'm getting the following error:
Notice: Undefined index: hid in E:\Program Files\xampp\htdocs\cont.php
echo "<table align='right'><tr><td><a href='cont.php?hid=101'><input type='button' value='Account Settings'></a></td></tr></table>";
cont.php code:
$con = mysql_connect("localhost","Rahul","");
mysql_select_db("ebusiness", $con);
if($_SESSION['id']==1)
{
include 'business.php';
}
else if($_GET['hid']==101)
{
session_start();
include 'edprofile.php';
}
You are directly checking on $_GET['hid'] without checking if it is set or not.
else if(isset($_GET['hid']) && $_GET['hid'] == 101)
Try this code :
added : $hid = isset($_GET['hid'])?$_GET['hid']:"";
edited : $_GET['hid'] to else if($hid==101)
$con = mysql_connect("localhost","Rahul","");
mysql_select_db("ebusiness", $con);
$hid = isset($_GET['hid'])?$_GET['hid']:"";
if($_SESSION['id']==1)
{
include 'business.php';
}
else if($hid==101)
{
session_start();
include 'edprofile.php';
}
Change this line:
else if($_GET['hid']==101)
to:
else if(isset($_GET['hid']) && $_GET['hid']==101)
Remember that when you are using GET or POST on your page, there is no guarantee that a certain variable has been sent, as the HTTP protocol is stateless by default.
It is there for important to checn that a variable exists, and also that the variable is validated. To this end the isset() method is quite useful. I would recommend you do a
isset($_GET['hid'])
in your code in an if statement.
you are also missing to pass hid variable in you get request ( in URL). check it out. It shuld content ?hid=101 or &hid=101 some where.
and replace your else if condition with
else if(isset($_GET['hid']) && $_GET['hid']==101)
Related
how to jump a page if condition true and also i want to sent a variable value to secont page
if(isset($_POST['compair']))
{
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
$mail=$_SESSION['usermail'];(i want to sent "$mail" variable)
header("Location:resetpass.php?value = $mail ");
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}
please also tell me how to receive this variable on second page.
Your solution is correct. Just pay attention to spaces:
header("Location: resetpass.php?value=$mail");
exit; // as suggested by "nogad"
Also make sure that resetpass.php file is in the same directory of current page.
In resetpass.php you can get the variable by $_GET['value'] like:
<?php
if( isset($_GET['value']) ){
$mail = $_GET['value'];
}
After header("Location : resetpass.php?value=$mail");
exit(); // i.e quit the current page and go to resetpass.php
Then at resetpass.php collect $mail using the GET method.
if(isset[$_GET['value'])){
$the_mail = $_GET['value'];
}
I am writing from my handy. So apologies for any layout discripancies
I made a script that shows the value of "school_id" in url parameter.
http://mywebsite.com/mygrade?school_id=00000
I use $_GET['school_id'] to display the ID number.
<?php echo $_GET['school_id']; ?>
But I what I want is if the parameter "school_id" is empty, I want to display the previous data entered.
Example, the user already browse http://mywebsite.com/mygrade?school_id=00000 then he browse http://mywebsite.com/mygrade?school_id= which id has no value. It will still display 00000 which is the previous ID he used.
I used this code below but doesn't work.. :(
<?php
session_start();
$_SESSION['schoo_id'] = $_GET['school_id'];
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_GET['school_id'];
}
?>
Anyone who get my point and could help me?
I'm going to break this down line by line, please let me know in the comments if I need to explain anything further:
Self explanatory:
<?php
session_start();
There is a typo here:
$_SESSION['schoo_id'] = $_GET['school_id'];
But! Fixing it won't resolve your problem. What happens if $_GET['school_id'] is not defined/blank? Guess what, $_SESSION['school_id'] is now blank. Obviously you don't want this behavior, so you'll want to only set $_SESSION['school_id'] if $_GET['school_id'] is defined
accessing $_GET['school_id'] will throw an E_NOTICE error if it isn't defined, so you'll want to instead check its existence, rather than checking to see if it is null.
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
Oh, that typo was intended. Why misspell school though? No need! :)
echo $_SESSION['schoo_id'];
What is this doing? Nothing! No echo, nothing. Just accessing a variable and doing nothing with it.
}
else{
$_GET['school_id'];
}
?>
Here's what your code should look like, or at least I believe is what you intend:
<?php
session_start();
if (isset($_GET['school_id']) && $_GET['school_id'] !== ""){
$_SESSION['school_id'] = $_GET['school_id'];
}
// $_SESSION['school_id'] will be guaranteed to be what $_GET['school_id'] is (if set)
// or whatever it was last time it was defined
// always echo it.
echo $_SESSION['school_id'];
?>
<?php
session_start();
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_GET['school_id'];
$_SESSION['schoo_id'] = $_GET['school_id']; //here set the session
}
?>
I agree with Salman A, the simplest way:
<?php
session_start();
if (is_int($_GET['school_id'])) $_SESSION['school_id'] = $_GET['school_id'];
// further use $_SESSION['school_id'] for your needs.
?>
what you need to do here is save the GET value in SESSION only if it is set for later use so this should work
<?php
session_start();
if (!isset($_GET['school_id']) || $_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_SESSION['schoo_id'] = $_GET['school_id'];
echo $_GET['school_id'];
}
?>
You almost have it.
<?php
session_start();
if (isset($_GET['school_id']) && trim($_GET['school_id']) !== '') {
// its a fair assumption to make that 'school_id' is intended to be an integer,
// however I will not make that assumption on the OP's behalf.
$_SESSION['school_id'] = $_GET['school_id'];
}
if (isset($_SESSION['school_id']) {
echo $_SESSION['school_id'];
}
else {
echo 'have not entered a school id yet';
}
?>
Have I missed something here that I'm not seeing that would throw an undefined index error for this code? In testing a addition of code to functions.php where $_POST['sub'] is not being passed it throws the undefined index error below, but this same isset() test against the exact same POST variable "sub" is performed about 12 times above line 494 without throwing error. What am i missing?
ERROR FROM PHP
Notice: Undefined index: sub in
/home/path/public_html/dtest/includes/functions.php on line 494
CODE FOR LINE 494
if (isset($_POST['sub']) && $_POST['sub'] == "ritem") {
$id = $_POST['ritemid'];
unset($_SESSION['cart']['items'][$id]);
header("Location: ".$_SERVER['HTTP_REFERER']."");
die();
} else {
echo $_POST['sub'];
}
Remove the echo $_POST['sub']; from the else part which is responsible for this Undefined index notice and replace with the echo statement.
Should be like this..
<?php
if (isset($_POST['sub']) && $_POST['sub']=="ritem") {
$id=$_POST['ritemid'];
unset ($_SESSION['cart']['items'][$id]);
header("Location: ".$_SERVER['HTTP_REFERER']."");die();}
else
{
echo "The subject is not set";
}
That is because.. when the if fails which means the $_POST['sub'] is not set , so when it comes to the else part , you are trying to output $_POST['sub'] which was actually not set (which is actual source of this problem)
Its because of else case
if (isset($_POST['sub']) && $_POST['sub']=="ritem") {
$id=$_POST['ritemid'];
unset ($_SESSION['cart']['items'][$id]);
header("Location: ".$_SERVER['HTTP_REFERER']."");
die();
} else if(isset($_POST['sub'])) {
echo $_POST['sub'];
} else {
// Do something else here
}
I need some help with this I have for example index.php and and i need to make it something like.
someone access:
index.php?search=blbla
include search.php
else
include home.php
I need an advice with this thanks
Try this
if (isset($_GET['search'])) include('search.php');
else include('home.php');
Well, you could use isset() to see if the variable is set. e.g.
if(isset($_GET['search'])){
include "search.php";
}
else {
include "home.php";
}
$sq = $_GET['search']; //$_GET['']
if (isset($sq) && $sq != '') {
include('search.php');
} else {
include('home.php');
}
I personally prefer to check if a $_GET is set and if it actually equals something like so:
if(isset($_GET['search']) && strlen(trim($_GET['search'])) > 0): include 'search.php';
else: include 'home.php';
This will avoid the problem of putting in the $_GET variable but not actually setting it.
use it like this
if (isset($_GET['search']))
include 'search.php';
else
include 'home.php';
<?php
//if($_GET['search'] > ""){ // this will likely cause an error
if(isset($_GET['search']) && trim($_GET['search']) > ""){ // this is better
include ('search.php');
}else{
include ('home.php');
}
?>
When using isset() you need to be aware that with an empty GET variable like this script.php?foo= that isset($_GET['foo']) will return TRUE
Foo is set but has no value.
So if you want to make sure that a GET variable has a value you might want to use strlen() combined with trim() instead...
if (strlen(trim($_GET['search'])) > 0) {
include('search.php');
} else {
include('home.php');
}
Also you might want to use require() instead of include(). A PHP fatal error is generated if search.php cannot be "required" with just a PHP warning if search.php cannot be "included".
I'm running into a server notice that doesn't seem to effect the loading of my pages but nonetheless creates a new entry in the error log every time a page is loaded... That error is:
PHP Notice: Undefined index: thing in C:\File Location\htdocs\index.php on line 1
I'm not sure whether the problem is actually on the first line or on a subsequent line, so I included a modified version of the whole file. The weird thing for me is that there's an identical line of code on several other files and it doesn't raise an issue in them. Also, the value is correctly extracted and all is well, I just don't know what to change in order to avoid the notice.
$thingvalue = $_REQUEST['thing'];
include("mdetect.php");
$iphoneTierHomePage = 'mobilemain.php';
$iphoneTierMobilePage = 'mobilepage.php?thing=' . $thingvalue;
$genericMobileDeviceHomePage = 'mobilemain.php';
$genericMobileDeviceMobilePage = 'mobilepage.php?thing=' . $thingvalue;
$line1 = define('WP_USE_THEMES', true);
$line2 = require('./wp-blog-header.php');
$desktopPage == $line1 + $line2;
$uagent_obj = new uagent_info();
function AutoRedirectToProperHomePage()
{
global $thingvalue, $uagent_obj, $iphoneTierHomePage, $genericMobileDeviceHomePage, $iphoneTierMobilePage, $genericMobileDeviceMobilePage, $desktopPage;
if ($thingvalue == ''){
if ($uagent_obj->isTierIphone == $uagent_obj->true)
header ('Location: '.$iphoneTierHomePage);
else if ($uagent_obj->DetectMobileQuick() == $uagent_obj->true)
header ('Location: '.$genericMobileDeviceHomePage);
else
header ('Location: '.$desktopHomePage);
}
if ($thingvalue != ''){
if ($uagent_obj->isTierIphone == $uagent_obj->true)
header ('Location: '.$iphoneTierMobilePage);
else if ($uagent_obj->DetectMobileQuick() == $uagent_obj->true)
header ('Location: '.$genericMobileDeviceMobilePage);
else
header ('Location: '.$desktopPage);
}
}
AutoRedirectToProperHomePage();
It's referring to the array index for the first line in index.php:
Try this:
$thingvalue = empty($_REQUEST['thing']) ? '' : $_REQUEST['thing'];
It is trying to reference the index thing inside the $_REQUEST superglobal. If someone is viewing that page directly and was not posted via a form or directed with a ?thing=foobar in the query string, PHP will show that notice. I'd recommend not using $_REQUEST as it checks both $_GET and $_POST which is not very secure/practical - then check if it is set, and if not, taking some failsave action:
try
{
if(!isset($_POST['thing']))
{
throw new Exception('No direct access. Please use our Contact form');
}
else
{
$thingvalue = $_POST['thing'];
}
}
catch(Exception $e)
{
echo $e->getMessage();
exit();
}
The issue is that you are trying to get the value of thing here
$thingvalue = $_REQUEST['thing']; before checking if the value exists first.
try this first
if( !isset( $_REQUEST['thing']) )
{
do something because its missing
}