This is my URL :
http://localhost/messources/actes/palactes.php?mode=MODE_NEW
In php I have this :
if (isset($_GET['id']))
{
$_SESSION['id'] = $_GET['id'];
}else{
$_SESSION['id'] = $_SESSION['id'];
}
since id is not in the URL, I would expect this entire block not to be executed. $_GET['id'] doesn't exist, so the value should be NULL therefore isset() testing that.
However, I get a php notice :
Undefined index: id in ....
What should I do to get rid of that notice (I don't want to add &id= to the url)
Use
session_start();// Only if you havent started.
if (isset($_GET['id']))
{
$_SESSION['id'] = $_GET['id'];
}else{
$_SESSION['id'] = 0;
}
And you can check if its 0 or not . If 0 then, NOt logged in else logged in.
There is no need of - $_SESSION['id'] = $_SESSION['id'];.
Do this -
if (isset($_GET['id']))
{
$_SESSION['id'] = $_GET['id'];
}
Your reading from id in the session is undefined
$_SESSION['id'] = $_SESSION['id'];
According to your question,
What should I do to get rid of that notice (I don't want to add &id=
to the url)
And your URL is
http://localhost/messources/actes/palactes.php?mode=MODE_NEW
Your PHP code has
if (isset($_GET['id']))
{
$_SESSION['id'] = $_GET['id'];
}else{
$_SESSION['id'] = $_SESSION['id'];
}
1) How do you assign a value to your $_GET['id'] because with what you have on your URL $_GET['id'] will always return NULL since it has no values from anywhere. You can test by doing var_dump($_GET); and your $_SESSION['id'] will also return NULL. Test again using var_dump($_SESSION);. You must give it a value before you can get anything assigned to $_GET['id']. See http://php.net/manual/en/reserved.variables.get.php
This is the solution :
if(!isset($_SESSION['id']) && !isset($_GET['id'])){
$_SESSION['id'] = NULL;
}
if(! isset($_SESSION['id']) && isset($_GET['id'])){
$_SESSION['id'] = $_GET['id'];
}
This doesn't run when $_SESSION['id'] is set.
The function you are looking for is empty , that checks if it exists and is not one of the following values:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
So as I would rewrite your code is
if (!empty($_GET['id'])) {
$_SESSION['id'] = $_GET['id'];
} else {
$_SESSION['id'] = $_SESSION['id'];
}
Related
I am new to php and have been reading on every forum about this issue but nothing works it still gives me the warning: undefined index. Any help would be greatly appreciated.
if(isset($_POST['redeem_points'])){
$id = $_POST['id'];
$points = mysqli_real_escape_string($conn,$_POST['points']);
$sql = "UPDATE users set user_points = user_points - '".$points."' WHERE user_id = '". $id ."' ";
$result = mysqli_query($conn,$sql);
if ($result === TRUE) {
echo '<script> window.alert("Points Redeemed successfully !")
location.href = "admin_manage_points.php" </script>';
}
else{
echo "failed";
}
}
use isset() on $_POST array elements, to make sure they are set, e.g.:
$id = isset($_POST['id']) ? $_POST['id'] : false;
If you use PHP 7 or higher you can use the following:
// returns the value of $_POST['id']
// or returns false if it does not exist.
$id = $_POST['id'] ?? false;
The null coalescing operator (??) returns the first operand if it exists and is not NULL; otherwise it returns its second operand.
You check an other index than you use.
isset($_POST['redeem_points']) // this will be checked
$id = $_POST['id']; // that is was you are using
try this:
$id = (int) $_POST['id'];
And i was wondering, where does "undefined index error" come from? php or javascript?
Because your javascript code has error too. It should like this;
echo '<script> window.alert("Points Redeemed successfully !");
location.href = "admin_manage_points.php" </script>';
I get a field value using GET from a URL.
www.test.com?id=12345
I create a session using assigning the id to a variable.
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
if(isset($_SESSION['mainid'])) {
echo "Your session is running " . $_SESSION['mainid']; }
I then use echo session on other pages
if(isset($_SESSION['mainid'])) {
echo "Your session is running " . $_SESSION['mainid']; }
The problem is when i go back to the home page, it tries to look for the get value $id, and it is blank now as the URL doesn't contain the Feild value id=12345. How do i get around this ?
if(!isset($_SESSION)){
session_start();
}
if(!isset($_SESSION['mainid'])){
$id= $_GET['id'];
$_SESSION['mainid'] = $id;
} elseif(isset($_GET['id']) && isset($_SESSION['mainid'])) {
if($_GET['id'] != $_SESSION['mainid'] )
{
$_SESSION['mainid'] = $_GET['id'];
}
}
Well, you should rewrite your code for sure to check if the variable exists first and then start the session.
Something like this should do.
$id = isset($_GET['id']) ? $_GET['id'] : false;
if ($id) {
session_start();
$_SESSION['mainid'] = $id;
}
Just put your below code
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
inside isset
Your code should look something like below code:
if(isset($_GET['id'])){
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
}
I have a problem in carrying session values between the pages.
I was struggling for 3 days for this issue.
Help me to overcome from this issue.
index.php (login page):
// initially declaring a variable with null value
!! include "conn.php";
#session_start();
if(isset($_SESSION['uname']))
{
$_SESSION['uname'] = " ";
}
else
{
$_SESSION['uname'] = " ";
}
?>
//later assigning the value
$usrname = $_POST['uname'];
$pass = $_POST['pass'];
$chk = mysqli_query($con,"select * from members WHERE username='$usrname'");
while($value = mysqli_fetch_array($chk))
{
$realpassword = $value['password'];
$_SESSION['uname'] = $_POST['uname'];
}
if(!isset($realpassword))
{
$realpassword = "";
}
if($realpassword == $pass)
{
echo "<script>window.location.assign('dashboard.php');</script>";
}
Dashboard.php (Dashboard):
// In dashboard
#session_start();
include "conn.php";
if(isset($_SESSION['uname'])&&$_SESSION['uname']!="")
{
$uname =$_SESSION['uname'];
}
else{
echo "<script>window.location.assign('http://www.website.com');</script>";
}
/// This page working fine
In page 3:
/// Session value not carried into this page .. when this page loads automatically logouts and redirect into home page
session_start();
include "conn.php";
if(!isset($_SESSION['uname'])&&$_SESSION['uname']=="")
{
echo "<script>window.location.assign('http://www.website.com');</script>";
}
$uname =$_SESSION['uname'];
You wrote :
if(!isset($_SESSION['uname']) && $_SESSION['uname'] == "")
{
echo "<script>window.location.assign('http://www.website.com');</script>";
}
Should be (OR not AND) :
if(!isset($_SESSION['uname']) OR $_SESSION['uname']==""){
echo "<script>window.location.assign('http://www.website.com');</script>";
}
Your code as pseudo code
index.php:
1 start a session
2 if uname is set in session, set it to one space
3 otherwise, set it to one space
4 get data from db
5 if we have data, set uname in session to POST data uname
in "page 3":
1 if uname is NOT set in session OR uname in session is empty string
2 logoff
3 otherwise
4 proceed ...
according to the other answer and 2) - 3) in index, Condition in 1) in "page 3" is never true. And in dashboard, you may see similar problems resulting from "deleting" the $_SESSION['uname'] with one space " " and checking for empty string ""
Change index.php:
include "conn.php";
#session_start();
unset($_SESSION['uname']); // delete previous values unconditionally (!)
I want to access the data in the php array. I have tried array[0] and array['Name'] but i dont get an output. here is my code:
session_start();
$user = $_SESSION['username'];
$sql_1 = "SELECT UserID FROM users WHERE username=$user";
$result_1 = mysqli_query($sql_1);
$uID = mysqli_fetch_array($result_1);
if ($uID=NULL) {
echo 'null';
} else {
echo $uID[0];
}
Now I am not getting any output from the echo command. So what am i doing wrong here?
The Part if ($uID=NULL) is always true[UPDATED:false], because you're doing an assignment rather than a comparism (that would be if ( $uID == NULL ))
I am creating a login script that stores the value of a variable called $userid to $_SESSION["userid"] then redirects the user back to the main page (a side question is how to send them back where they were?).
However, when I get back to that page, I am echoing the session_id() and the value of $_SESSION["userid"] and only the session id shows up. It had occurred to me that maybe my redirect page needs to have at the top, but if this were true, then the session_id I'm echoing would change each time I end up on the page that is echoing it. Here is the script:
<?php
session_start();
include_once("db_include.php5");
doDB();
//check for required fields from the form
if ((empty($_POST['username']) && empty($_POST['email'])) || empty($_POST['password'])) {
header("Location: loginform.php5");
exit;
} else if($_POST["username"] && $_POST["password"]){
//create and issue the query
$sql = "SELECT id FROM aromaMaster WHERE username='".$_POST["username"]."' AND password=PASSWORD('".$_POST["password"]."')";
$sql_res =mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
//get the number of rows in the result set; should be 1 if a match
if(mysqli_num_rows($sql_res) != 0) {
//if authorized, get the userid
while($info = mysqli_fetch_array($sql_res)) {
$userid = $_info["id"];
}
//set session variables
$_SESSION['userid'] = $userid;
mysqli_free_result($sql_res);
//redirect to main page
header("Location: loginredirect.php5");
exit; }
} else if($_POST["email"] && $_POST["password"]) {
//create and issue the query
$sql = "SELECT id FROM aromaMaster WHERE email='".$_POST["email"]."' AND password=PASSWORD('".$_POST["password"]."')";
$sql_res =mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
//get the number of rows in the result set; should be 1 if a match
if(mysqli_num_rows($sql_res) != 0) {
//if authorized, get the userid
while($info = mysqli_fetch_array($sql_res)) {
$userid = $_info["id"];
}
//set session variables
$_SESSION['userid'] = $userid;
mysqli_free_result($sql_res);
//redirect to main page
header("Location: loginredirect.php5");
exit;}
} else {
//redirect back to login form
header("Location: loginform.php5");
exit;
}
mysqli_close($mysqli);
?>
You're doing this:
while($info = mysqli_fetch_array($sql_res)) {
$userid = $_info["id"];
}
Where you should do this:
while($info = mysqli_fetch_array($sql_res)) {
$userid = $info["id"];
}
Make sure:
<?php
session_start();
Is at the top of each page.
Additionally, you can test by commenting out your redirects and echo'ing the value you're setting with to make sure you're retrieving/storing the correct value to begin with.
You need to call session_write_close() to store the session data changes.
Side answer: you can use the $SERVER["HTTP REFERER"] to redirect back, if it was filled by the browser