0

function send() {
    event.preventDefault();
    $element.delay(5000).fadeIn();                                  
    function pausing() {                                        . 
    var cat = 1;
    var params = 'dog='+cat;
    var xhr = new XMLHttpRequest();
    
    xhr.open("POST", "page.php");
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(params);
    
    }
    var timeout = setTimeout(pausing, 5000);                     
}

htmlelement.addEventListener('click', send);

I`m trying to call a session variable from another page and set it equal to a variable to be run in an if statement. I saw a post about passing into functions that used global or passing to function as function($variable) this did not pass into the if statement.

<?php
session_start();
$tag = $_SESSION['hello'];
$dog = $_POST['dog'];

if ($dog == 1) {
  echo $tag; //does not pass
}

$GLOBAL['apple'] = $_SESSION['hello'];
//or $GLOBAL['apple'] = $tag;

if ($dog == 1) {
  echo $GLOBAL['apple']; //does not pass
}

//also tried

if ($dog == 1) {
  globals $tag
  echo $tag; //does not pass
}

session_destroy();
Rob
  • 11
  • 3
  • Not sure if this help but variable dog is set equal to a post from java script. $dog = $_POST [ 'doggy' ] – Rob May 06 '21 at 00:25
  • 2
    Have you executed `session_start()` before this code? Make sure you're seeing any and all errors reported. [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – Phil May 06 '21 at 00:28
  • Hello, yes session_start ( ) is set at start of both pages. I`m going to check out that link. – Rob May 06 '21 at 00:35
  • 1. Where are the functions that you speak of? 2.`$dog` always == 1 because that what you set it to. 3. its `$GLOBALS` with an `S` ... maybe more can't tell what you're doing. – AbraCadaver May 06 '21 at 01:04
  • That link is going to save me a lot of time in the future. So the variable $dog I get from the java script does not send a value until an on click event is ran. So when the page first runs variable dog would be undefined until the click event is run. I checked and does on click take the value and goes to IF statements. $Global was a typo in the question but, I will re-run it just in case. – Rob May 06 '21 at 15:13
  • Re-ran with correct $GLOBALS although not passing. I checked out this question, "Store javascript data into PHP SESSION VARIABLE?". I`m thinking because, php runs before javascript when the post is sent to php page from javascript it is removing the session variable $_SESSION[ ' hello ' ]; – Rob May 07 '21 at 13:41

2 Answers2

0

//A Ajax request to animals.php
$.get('animals.php', function(data) { //data returns response from animals.php could just be an echo in the php page.
    $('#element').html(data); //select element from html using jquery.
});

//PHP page creating a session variable called Generate.php
<?php
session_start();

$_SESSION['ROB'] = 4;

session_destroy(); //okay to have it here cause this page does not handle the ajax request. 
?>

//PHP page to handle the ajax request called animals.php
<?php
session_start();

echo $_SESSION['ROB']; //Before AJAX request this will echo 4
//and will run any code wanted to use for $_SESSION['ROB'];

session_destroy(); //however before the AJAX request this line of code will run too!
//So when AJAX request is made the session variable 'ROB' will already be set to null
//not the original $_SESSION['ROB']; created on Generate.php page.
//By removing session_destroy(); here the session will be left open to be re-ran on each ajax request.
?>

Hello all, I`m very new at PHP and java script 3 months in but, I will try to explain the fix to my error as best as possible.

Normally when session_start(); is called on each PHP page it will store session variables created on every page to be used on every other page that has session_start(); as well. Even if session_destroy(); is called at the end of the PHP page.

However in my case, an AJAX request made to the PHP page that has session_destroy(); at the bottom. Was removing the set value for the session variable created on a separate PHP page.

After removing the session_destroy(); at the bottom of the PHP page the ajax request was made to. The session variables stayed set.

If there is any security concern with this please! comment.

Rob
  • 11
  • 3
-1

try adding session_start() before operating with sessions

Safaar
  • 24
  • 5