0

I'm trying to set a PHP $_SESSION variable when a user clicks a record in a modal table.

The modal table is set up by doing a while loop through records in a SQL Server table. It appears like follows:

    Value   |  Link
---------------------------
    0       |   <a href= changeVar.php?ficheNum=0></a>
    1       |   <a href= changeVar?ficheNum=1></a>
    2       |   <a href= changeVar?ficheNum=2></a>
    ...

When one of the links in the table is clicked it runs this code:

<?php
$ficheNum=$_REQUEST['ficheNum'];
$_SESSION['ficheCount'] = $ficheNum;

header("Location: test.php"); 
?>

The test.php page is simply a page of inputs that are populated with values from a database table. It also has the modal table that was shown above. The PHP on test.php is as follows.

$fiche_array = array();

$sel_query = "SELECT fiche
              FROM test.dbo.[TLM.Fiche_Globale]
              ORDER BY fiche ASC;";
$sel_result = sqlsrv_query($con, $sel_query) or die( print_r( sqlsrv_errors(), true));
while($sel_row = sqlsrv_fetch_array($sel_result)) {
    array_push($fiche_array, $sel_row['fiche']);
}

if(isset($_POST['new']) && $_POST['new']==1)
{
    $_SESSION['ficheCount'] ++;
}else if(isset($_POST['new2']) && $_POST['new2']==1)
{
    $_SESSION['ficheCount'] = $_SESSION['ficheCount'] - 1;
}else if(isset($_SESSION['ficheCount']) && $_SESSION['ficheCount'] > 0)
{
    $_SESSION['ficheCount'] = $_SESSION['ficheCount'];
}else{
    session_start();
    $_SESSION['ficheCount'] = 0;
}

The first two statements in the if/else statement are to operate next and previous record buttons. They work fine. The final statement is the code that runs if none of statements 1-3 are triggered. The third statement is supposed to trigger if the user is being redirected back to the page from from the changeVar.php page. However, this never triggers. Or, it triggers and doesn't do anything -- I'm not sure.

I've tried manually testing the changeVar page:

<?php
$ficheNum=$_REQUEST['ficheNum'];
$_SESSION['ficheCount'] = $ficheNum;

echo $_SESSION['ficheCount'];
?>

It sets the variable to the correct number but, when I return to the test.php page it always defaults back to the ficheCount that was set before as if I never set the variable in the changeVar.php page.

If anyone can see what I'm doing wrong here it would be greatly appreciated.

cheers,

1 Answer 1

3

In order to use PHP sessions, you always need to run the command session_start(); at the start of each PHP file that uses sessions. http://php.net/manual/en/function.session-start.php

Sign up to request clarification or add additional context in comments.

1 Comment

ouch. It was that easy. Added 'session_start();' to the top of the changeVar.php page and we're back in business. Thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.