Here is my PHP code:
<?php
session_start();
if ( !isset($_SESSION['index_visited']) )
{
$_SESSION['page2_visited'] = 1;
$_SESSION['wrong_number'] = 1;
header('Location: index.php');
}
?>
I think that $_SESSION['page2_visited'] = 1 and $_SESSION['wrong_number'] = 1 never get set. Program redirect me on index.php and thats it.
What I have to do to set it?
It's probably the session doesn't have time to properly save itself before the redirect happens.
Use session_write_close(); to force the saving of the session right before the header redirect.
So it would be:
<?php
session_start();
if ( !isset($_SESSION['index_visited']) ) {
$_SESSION['page2_visited'] = 1;
$_SESSION['wrong_number'] = 1;
session_write_close();
header('Location: index.php');
}
?>
session_commit() works too. It's just as alias of session_write_close()
i bet $_SESSION['index_visited'] is set. perhaps you want
if(isset($_SESSION['index_visited']) and $_SESSION['index_visited'] == 1) {
remember, if $_SESSION['index_visited'] has been set to 0, that counts as set.
The header('Location: index.php'); overwrites the session setting headers.
The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type.
http://php.net/manual/en/function.header.php
So please try: header('Location: index.php', false);
My mistake. This is not place for $_SESSION['wrong_number'], now it is working. Thank you for answers anyway.
Related
I have a session variable that is updated when a link is clicked which adds GET variables to the current URL:
<?php
$_SESSION['test'] = 0;
if (isset($_GET['do'] && $_GET['do'] == 'this')) {
$_SESSION['test'] = $_SESSION['test'] + 1;
}
echo $_SESSION['test'];
?>
Click me
When the link is clicked, the new session value that is echoed is still 0.
Why? How do I get this to echo the updated value? (1)
EDIT: Yes, session_start() is included at the top of the page.
change it to be like this:
<?php
session_start();
if(!array_key_exists('test', $_SESSION)){
$_SESSION['test'] = 0;
}
if(array_key_exists('do', $_GET) && $_GET['do'] === 'this') {
$_SESSION['test']++;
}
echo $_SESSION['test'];
?>
Click me
I like array_key_exists() more than isset because the key can exist being null and still pass the test:
From the php docs: "isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does."
You also were using isset incorrectly to test the contents of $_GET['do']. To top things off, you didn't start the session on each page load.
session_start() will initialize the $_SESSION variable and fill it with the appropriate contents. Since this was not in your code, the $_SESSION variable never had your 'do' key.
The second-to-last problem you had was the very first line, you were always setting the 'test' key to 0 causing the outcome to always be 1 after clicking Click me
and finally, you didn't close the php tag prior to outputting raw HTML which should have given you an error
Maybe you have not paste the whole code but here is how it should have been done.
<?php
session_start();
$_SESSION['test'] = 0;
if (isset($_GET['do']) && $_GET['do'] == 'this') {
$_SESSION['test'] += 1;
}
echo $_SESSION['test'];
?>
Click me
You need to start with session_start() Google to learn more about it.
You have put everything in the isset() Google isset() to learn more about it.
you need to close php tags before you can insert raw HTML. Inside HTML you can execute php with <?php ?> tags.
Hope this helps.
As mentioned before, you have everything wrapped in isset() should be
isset($_GET['do'])
not
isset($_GET['do']
Also, you're session is always being set to 0 on each page refresh. You need to only set $_SESSION['test'] = 0 if $_SESSION['test'] has not been set yet.
if (!isset($_SESSION['test'])) {
$_SESSION['test'] = 0;
}
You are missing session_start() at the top of your script. You're also not assigning the session if it doesn't previously exist, you're continuously setting it to 0 before incrementing.
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.
I was curious to know if i could do this, but found no examples on line
$info_find = mysql_query("SELECT info FROM sets WHERE category = '$selected_cat'")
or die( header ("location: index.php"));
Firstly doing it as above doesn't work. Can it be done?
Are there any drawbacks?
die() just writes its parameter out, it doesn't execute it. So a "or die()" construct will not help you there.
You can consider something like
or die(createRedirect("index.php"));
with
function createRedirect($where) {
$s='<html><head>';
$s.='<meta http-equiv="refresh" content="1; url='.$where.'">';
$s.='</head><body>If you are not automatically redirected click here';
$s.='</body></html>';
return $s;
}
if you are willing to accept the downsides of client-sided redirection
You can rewrite your code as
$info_find = mysql_query("SELECT info FROM sets WHERE category = '$selected_cat'") ;
if ($info_find === FALSE) {
header ('location: index.php');
die();
}
But, before use the header function, be sure you haven't send any output to the browser.
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.
On my site, forms are brought in via AJAX and checked against a sessionid. I know this is not optimal, but it's working for us. If the referrer doesn't have the session ID they are redirected back to "anotherpage". I need to allow some outside URL's access the form directly.
we set the sessionid on the page with the link to the form.
Here is what we have now on the form page:
<?php
$code = $_GET['sessionid'];
if(strcmp( $code , 'XXXXX' ) != 0) {
header("Location: http://www.domain.com/anotherpage.php");
}
?>
I need to allow some outside domains direct access to the form page and am having issues with this:
(I'm putting it above the head tag on the form page)
<?php
$code = $_GET['sessionid'];
$referrer = $_SERVER['HTTP_REFERER'];
if(strcmp( $code , 'XXXXX' ) !=0) {
header("Location: http://www.domain.com/anotherpage.php");
} else {
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.domain.com/desiredpage.php');
}
}
?>
this still bounces me back to "anotherpage.php" any ideas?
********EDIT*******
thx for the help, it works ad I requested. Now I see what I asked wasn't entirely correct. This appends the URL with =sessionid?=XXXXX. This isn't an issue on my site because I'm loading the content with .jquery .load so the URL doesn't change. I don't want the sessionid to be visible, and now it is. Can I either a) "trim" the url somehow or b) separate the two functions so they are exclusive?
if(strcmp( $code , 'XXXXX' ) !=0) {
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.domain.com/desiredpage.php');
} else {
header("Location: http://www.domain.com/anotherpage.php");
}
}
As I read your post, you want anyone from the preg_match to get the desired page regardless of sessionID status, so you don't want to test sessionID first.
Start the if block with the preg_match test.
Your first if is checking to see if they don't have the $code and redirecting them. This will always be the case. You should probably check the $referrer first and then do the $code check.
Try reverse if with else
<?php
$code = $_GET['sessionid'];
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/site1.com/", $referrer)) {
header('Location: http://www.domain.com/desiredpage.php');
} else if (strcmp( $code , 'XXXXX' ) != 0) {
header("Location: http://www.domain.com/anotherpage.php");
}
?>
If I'm not misunderstanding this, the problem is in the order in which you are checking things.
If you want to allow some referrers to access the site even if they don't have the session id, you have to check for that before checking for the session id. Otherwise, they will end up being treated just like everyone else.
You can either switch the order of the conditions (first check for the referrer and then check fo the session id) or check for the referrer inside the branch in which you already know the session id is not valid.
The issue could be in your regex, it should be:
if (preg_match("/site1\.com/",$referrer))
notice escaping the dot (.)