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;
}
Related
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'];
}
Unable to get session from different PHP page other than where i initialized it
This is my first PHP page where i initialize the session.
<?php
$i =1;
$team = htmlentities($_POST['team']);
$id = htmlentities($_POST['id1']);
$mobile = htmlentities($_POST['mobile1']);
if(isset($_POST['id2']))
{
$ids = htmlentities($_POST['id2']);
$mobiles = htmlentities($_POST['mobile2']);
$i=2;
}
if(isset($_POST['id3'])){
$ids = $ids.','.htmlentities($_POST['id3']);
$mobiles = $mobiles.','.htmlentities($_POST['mobile3']);
$i=3;}
echo $team;
echo $i;
$connect =new mysqli('localhost', 'root', 'password','test');
if($connect->connect_error)
{
die("connection failed : ".$connect->connect_error);
}
$data = "INSERT INTO `Users`(`team_name`, `id`, `mobile`, `ids`, `mobiles`) VALUES ('$team','$id','$mobile','$ids','$mobiles')" ;
$createData="CREATE TABLE `$id`(
`id` INT NOT NULL ,
`ansOpChoosen` INT NOT NULL,
`realAns` INT NOT NULL
);";
echo 'pass';
$link ="/test.html";
$link2 = "/signups.html";
if(mysqli_query($connect,$data) && mysqli_query($connect,$createData) )
{
session_start();
$_SESSION['user'] = $id;
header('Location: '.$link);
echo "new record created successfully";
}
else{
header('Location: '.$link2);
echo "error";
}
$connect->close();
?>
This is another php page where i try to retrive data but it doesnt fetch any thing
<?php
$id = $_SESSION['user'];
$quesNo = $_POST['questionNo'];
$optionCho = $_POST['optionchoosen'];
$optionReal =$_POST['optionreal'];
echo $id;
//echo "hbbhkhb";
$connect =new mysqli('localhost','root','password`','test');
if($connect->error){
echo "connection error";
}
$check ="SELECT * FROM `$id` WHERE `id`=$quesNo";
if($res=mysqli_query($connect,$check)){
$count = mysqli_num_rows($res);
if($count>0)
{
$data ="UPDATE `$id` SET `ansOpChoosen`=$optionCho,`realAns`=$optionReal WHERE `id`=$quesNo";
}
else{
$data = "INSERT INTO `$id`(`id`,`ansOpChoosen`,`realAns`) VALUES ($quesNo,$optionCho,$optionReal)";
}
$store=mysqli_query($connect,$data);
}
?>
Put session_start(); at the top of every page that you want to use sessions on.
You always have to call session_start() before doing something with the session.
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
Source: http://php.net/manual/en/function.session-start.php
A session is started with the session_start() function.
Be careful : it must be top of every page.
For example :
<?php
session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
?>
Manual : http://php.net/manual/en/session.examples.basic.php
I can't seem to get my session variable to output. It works fine on the other one I'm using:
$_SESSION['status']['register']['username'] = $username;
header('Location: register-form.php');
When you are taken to register-form.php it should output the variable for you:
<?php
if(isset($_SESSION['status']['register']['username'])){
$username = $_SESSION['status']['register']['username'];
} else {
$username = '';
}
?>
Exact same code is used on email_address and works fine.
Here is the same code for the email
$_SESSION['status']['register']['email_address'] = $email_address;
And on the form page:
<?php
if(isset($_SESSION['status']['register']['email_address'])){
$ea_value = $_SESSION['status']['register']['email_address'];
} else {
$ea_value = '';
}
?>
You miss to initialize your session with
session_start();
at top of your register_form.php
i have 2 files index.php and download.php
index.php :
<a href='download.php?id=$id'>Get file</a>
download.php :
<?php
if($id){
getting info from db
}
?>
and I get this error :
Notice: Undefined variable: id in download.php on line 2
May you help me ?
You need $id = isset( $_GET['id']) ? intval( $_GET['id']) : 0; at the top of your download script.
<?php
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
// getting info from db
}
?>
In your download.php change the if statement
if(!empty($_GET['id'])) {
$id = $_GET['id'];
}
The question is what is the value of your $id on the download.php?
on download.php it should be like this:
<?php
$id = $_GET["id"];
if($id){ getting info from db }
?>
<?php
$id = $_GET['ID'];
if($id)
{
getting info from db
}
?>
try this:
index.php :
'>Get file
download.php :
<?php
$id = $_GET['id'];
if($id){ getting info from db }
?>
try if ((isset ($_GET ['id'])) && ($id = intval ($_GET ['id']))) instead.
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