I am new to php, and very new to sessions, so I have no idea what I am doing wrong. I followed the tutorial on tizag, and put this code on my site:
<?php
session_start();
echo SID . "<br><br>";
if(isset($_SESSION['views'])) {
$_SESSION['views'] = $_SESSION['views'] + 1;
} else {
$_SESSION['views'] = 1;
echo "views = ". $_SESSION['views'];
}
?>
The SID changes whenever I refresh, and the number does not count up.
Update: Url: http://121.73.150.105/PIA/
FIXED BY: Putting session_start() before my doctype, title etc.
Are cookies enabled in you're browser ? phpsessid is stored as a cookie , you can set different parameters for it , one that could be usefull in you're case could be session_get_cookie_params() , and see if everithing is oki with the session cookie params .
If anything is wrong like expiration date you can set the params with session_set_cookie_params()
Your PHP setup may have been configured to not save sessions in cookies.
To verify if this is the case, you can take a look at session.use_cookies in your php.ini, or using ini_get, like so:
<?php echo ini_get('session.use-cookies'); ?>
You can correct it at runtime, as well, using ini_set, like so:
<?php ini_set('session.use-cookies', '1'); ?>
either you are outputting something to the browser before calling session start, or you have cookies disabled.
You don't output the $_SESSION['view'] after the if statement. I think that's why it doesn't change.
Try:
<?php
session_start();
echo SID . "<br><br>";
if(isset($_SESSION['views'])) {
$_SESSION['views'] = $_SESSION['views'] + 1;
} else {
$_SESSION['views'] = 1;
}
echo "views = ". $_SESSION['views'];
?>
So you always output the new $_SESSION['views'] value.
EDIT:
I think the right answer is that the session is not set. But I'm curious, how can the code always outputs "view = 1"? Can I open a new question referencing this question or just discuss it here?
in your code if you cant see session ID you can write
session_id() in place of SID.
ini_set("session.use_cookies",1);
ini_set("session.use_only_cookies",1);
this two parameter must set to gether if you want it work
Related
Hey guys I'm trying to pass a php variable to another page. I tried it with sessions but no result.
newspaper.php
$newspaper= $newspaper['newspath'];
print_r($newspaper);
this outputs:
path/to/the/newspaper.
Now I want to use the variable in the second page.
newspaperviewer.php
echo $newspaper;
$SESSION = $newspaper;
I tried the first one but no result. The second one seems to be faulty.
Hope you guys can help me out.
Session is what you are looking for. A session variable can store a value and use this value on all pages of your project.
First thing to do is to start session on each file on your project. You can do this like this example
<?php
session_start(); //declare you are starting a session
$_SESSION['newspaper'] = "New York Times"; //Assign a value to the newspaper session
?>
On the other file you can use the value of the session by trying something like this
<?php
session_start(); //always start session don't forget!!
echo $_SESSION['newspaper'];
// This will echo New York Times
?>
Store the variable after starting session on page A, like so:
// FIRST PAGE (foo.php)
session_start();
$_SESSION['name'] = 'Jack';
Now, on the second page (or any page that you want to have access to $_SESSION, simply do the same but pull the variable.
// SECOND PAGE (bar.php)
session_start();
$name = $_SESSION['name'];
$_SESSION['name'] = null; // Or use session_unset() to delete all SESSION vars.
And that's how you pass variables using $_SESSION.
Please use this code to set session
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
You can write like this
newspaper.php
session_start();
$newspaper= $newspaper['newspath'];
$_SESSION['newspaper'] = $newspaper;
Now you can use this session variable in
newspaperviewer.php
session_start();
$newspaper = $_SESSION['newspaper'];
echo $newspaper;
session_unset(); // remove all session variables
First
newspaper.php
$newspaper= $newspaper['newspath'];
//print_r($newspaper);
session_start(); //it starts your session here
$_SESSION['newspaper']=$newspaper; //it sets a session variable named as newspaper
Second
$newspaper= isset($_SESSION['newspaper'])?$_SESSION['newspaper']:''; //checks and sets value
echo $newspaper; //outputs value
For more see session_start
http://php.net/manual/en/session.examples.basic.php
First you will need to start the session by using session_start() at the top of your page. Second, a session variable is written like this: $_SESSION['foo'].
I suggest you read these pages to get a better understanding of what's going on.
http://php.net/manual/en/reserved.variables.session.php
http://www.w3schools.com/php/php_sessions.asp
I have following problem, I am trying to set cookies without any success. setcookie(); function returns true so it looks like it setting cookie however when I am trying to access it on the same or following page I get error 'Undefined Index....'
<?
session_start();
ob_start();
echo setcookie("order",$_SESSION['cart'],time()+3600,'/',NULL);
//added to see if Cookie is set
echo "<br/>";
var_dump($_COOKIE);
exit();
if($_GET['paypal'] == 1){
header("Location: /paypal-express-checkout/process.php");
}else{
header("Location: /insert_order.php");
}
ob_end_flush();
exit();
?>
next page follows like this
<?php
session_start();
include_once("../includes/inc_config.php");
include_once("../order.php");
include_once("config.php");
include_once("paypal.class.php");
#region POST
if(!isset($_GET['token'])) //Post Data received from product list page.
{
//Mainly we need 4 variables from an item, Item Name, Item Price, Item Number and Item Quantity.
if(!isset($_COOKIE['order'])){
exit();
}
$paypal_data = '';
$ItemTotalPrice = 0;
$order = unserialize($_COOKIE['order']);
print_r($order);
exit;
You are setting the domain value to NULL. Try leaving the NULL away:
echo setcookie("order",$_SESSION['cart'],time()+3600,'/');
OR set it to your domain:
echo setcookie("order",$_SESSION['cart'],time()+3600,'/',".yourdomain.com");
I would var_dump or print_r the $_COOKIE variable before I make a decision that it's not getting passed through. Holding that thought once you setcookie something gets registered into the $_COOKIE variable for sure.
I agree with the statements above since you only can access $_COOKIE on the next refresh but there is another way to do it to make your form or page more interactive.
I would register the cookie and use a php page refresh (display a working... div while thats happening) then come back to the page and try to do what you originally tried doing. Very basic but pretty much straight forward.
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
echo $_COOKIE["test"];
Notice:Undefined index:test in
D:\projects\Trainee2014\Sonali\php\cookiedemo.php on line 3
if possible please give me solution
<?php
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
$check = $_COOKIE["test"];
if (isset($check)){
echo 'Cookie not set';
}
else{
echo 'Cookie set';
}
?>
Try the above code to handle the notice.
Change your code to this and try,
setcookie("test","sonali",time()+3600,"../php/"," ",false,false);
echo isset($_COOKIE["test"]) ? $_COOKIE["test"] : '';
Problem is at the first time $_COO KIE["test"] is not set. so you need to refresh. in order to avoid this problem you should simply use the if condition with isset().
Try like this:
Syntax setcookie(name,value,expire,path,domain,secure);
Use like this in page :
<?php
$value = "cookie value";
// send a simple cookie
setcookie("user",$value);
OR
// send a cookie that expires in 24 hours
setcookie("user",$value, time()+3600*24);
?>
Access it on page like this :
<?php
echo $_COOKIE["user"];
OR
echo $HTTP_COOKIE_VARS["user"];
// Print all cookies
print_r($_COOKIE);
?>
Try this :
setcookie("test","sonali",time()+3600,"../php/",false,false);
print_r($_COOKIE);
echo $_COOKIE["test"];
Please set setcookes function only 5 parameter
setcookie(name, value, expire, path, domain);
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/php/', the cookie will only be available within the /php/ directory and all sub-directories such as /php/../ of domain. The default value is the current directory that the cookie is being set in.
Ref: http://php.net/setcookie
setcookie("test","sonali",time()+3600,"/"," ",false,false);
echo $_COOKIE["test"];
I think this code should echo "first" in first usage and after refresh it should echo timestamp,but it will show first every time,where is my problem?
I set cookie permition on always.
if (isset($_SESSION['TestSession']))
{
echo ($_SESSION['TestSession']);
}
else
{
echo "first";
$_SESSION['TestSession'] = time();
}
Have you placed
session_start();
at the top of your page?
To start a session, first session_start() should be called. Otherwise $_SESSION won't be set, and you will initialize it every time.
I'm trying to do a simple test php script for sessions. Basically it increments a counter (stored in $_SESSION) every time you refresh that page. That works, but I'm trying to have a link to destroy the session which reloads the page with the ?destroy=1 parameter. I've tried a couple of if statements to see if that parameter is set and if so to destroy the session but it doesn't seem to work.
I've even put an if statement in the main body to pop-up a message if the parameter is set - but it doesn't seem to be picked up.
I know I'm doing something silly (I'm a PHP newbie) but I can't seem to find what it is...
See code here:
<?php
if ($_POST['destroy']) {
session_destroy();
} else {
session_start();
}
?>
<html>
<head>
<title>Session test</title>
</head>
<body>
<?php
if (isset($_POST['destroy'])) {
echo "Destroy set";
}
$_SESSION['counter']++;
echo "You have visited this page " . $_SESSION['counter'] . " times" . "<BR>";
echo "I am tracking you using the session id " . session_id() . "<BR>";
echo "Click here to destroy the session.";
?>
I think you put
$_POST['destroy']
Instead of
$_GET['destroy']
You need to use a form if you'd like to use a $_POST variable. $_GET variables are stored in the URL.
By the way you can use
$_REQUEST['destroy']
which would work regardless if the data is passed in a POST or a GET request.
In the PHP Manual it has code snippet for destroying a session.
session_start();
$_SESSION = array();
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
session_destroy();
Yeah, you're going to want to do
if( $_GET['destroy'] == 1 )
or
if( isset($_GET['destroy']) )
I know I'm doing something silly (I'm a php newbie) but I can't seem to find what it is...
that is how you are going to learn a lot ;) enjoy it ...