PHP sessions - storing results from database query

  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

Ok, I've got some PHP script working on my site which is split into 2 sections. The first section starts a session and retrieves 4 variables that are passing in the URL query string. It stores these variables and passes them on when the visitor follows certain links.

The second bit of script checks for the presence of one of the variables in the URL, then queries a database to find the record and retrieve all the fields so that we can display different logos, phone numbers etc for different affiliates.

What I'd like to do is make the second bit of script part of the session so I don't need to include script on every link on every page to pass the affiliates details across.

Here's my script. Thanks in advance
Code: [ Select ]
<?php
// PART 1

//start up the session
session_start();
$aff = array();
//check if affiliate query string is present and store parameters in session
if($_REQUEST["affcode"] && $_REQUEST["campaign"] && $_REQUEST["medium"] && $_REQUEST["activity"])
{
//affiliate referral
$aff["affcode"] = htmlentities(trim($_REQUEST["affcode"]));
$aff["campaign"] = htmlentities(trim($_REQUEST["campaign"]));
$aff["medium"] = htmlentities(trim($_REQUEST["medium"]));
$aff["activity"] = htmlentities(trim($_REQUEST["activity"]));
$_SESSION['affiliate'] = $aff;
}
//if($_REQUEST["affcode"] && $_REQUEST["campaign"] && $_REQUEST["medium"] && $_REQUEST["activity"])

//build the hyperlink query string to add to the aspx links
$linkSuffix = (is_array($_SESSION['affiliate']) ? http_build_query($_SESSION['affiliate'], '', '&amp;') : '');

?>


<?php
// PART 2

$mysqli = new mysqli("localhost", "username", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$medium=(array_key_exists("medium",$aff) ? str_replace(' ','_',$aff["medium"]) : "default");
/* //query database for sourcebusID */
if ($result=$mysqli->query("SELECT * FROM aff_details where medium='".$mysqli->real_escape_string($medium)."' LIMIT 1")) {
//if no results resend query for default resultset
if($result->num_rows<1)
{    
    $medium="default";
    $result = $mysqli->query("SELECT * FROM aff_details where medium='".$mysqli->real_escape_string($medium)."' LIMIT 1");
}
$row=$result->fetch_assoc();
/* free result set */
$result->close();
}
else
{
die('Error in query: '.$mysqli->error);
}

?>
  1. <?php
  2. // PART 1
  3. //start up the session
  4. session_start();
  5. $aff = array();
  6. //check if affiliate query string is present and store parameters in session
  7. if($_REQUEST["affcode"] && $_REQUEST["campaign"] && $_REQUEST["medium"] && $_REQUEST["activity"])
  8. {
  9. //affiliate referral
  10. $aff["affcode"] = htmlentities(trim($_REQUEST["affcode"]));
  11. $aff["campaign"] = htmlentities(trim($_REQUEST["campaign"]));
  12. $aff["medium"] = htmlentities(trim($_REQUEST["medium"]));
  13. $aff["activity"] = htmlentities(trim($_REQUEST["activity"]));
  14. $_SESSION['affiliate'] = $aff;
  15. }
  16. //if($_REQUEST["affcode"] && $_REQUEST["campaign"] && $_REQUEST["medium"] && $_REQUEST["activity"])
  17. //build the hyperlink query string to add to the aspx links
  18. $linkSuffix = (is_array($_SESSION['affiliate']) ? http_build_query($_SESSION['affiliate'], '', '&amp;') : '');
  19. ?>
  20. <?php
  21. // PART 2
  22. $mysqli = new mysqli("localhost", "username", "password", "database");
  23. /* check connection */
  24. if (mysqli_connect_errno()) {
  25.   printf("Connect failed: %s\n", mysqli_connect_error());
  26.   exit();
  27. }
  28. $medium=(array_key_exists("medium",$aff) ? str_replace(' ','_',$aff["medium"]) : "default");
  29. /* //query database for sourcebusID */
  30. if ($result=$mysqli->query("SELECT * FROM aff_details where medium='".$mysqli->real_escape_string($medium)."' LIMIT 1")) {
  31. //if no results resend query for default resultset
  32. if($result->num_rows<1)
  33. {    
  34.     $medium="default";
  35.     $result = $mysqli->query("SELECT * FROM aff_details where medium='".$mysqli->real_escape_string($medium)."' LIMIT 1");
  36. }
  37. $row=$result->fetch_assoc();
  38. /* free result set */
  39. $result->close();
  40. }
  41. else
  42. {
  43. die('Error in query: '.$mysqli->error);
  44. }
  45. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

When you get your query result just throw it into a session variable.

PHP Code: [ Select ]
$_SESSION['query_row'] = $row;
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

Thanks for the reply.

ok, I've added

Code: [ Select ]
$_SESSION['query_row'] = $row;


to the script and used the following to display the values from the database:

Code: [ Select ]
echo $_SESSION['query_row']['value']


This works fine on the first page, but doesn't store this value, it just changes back to the default values if you browse away from this page, even though the script is included on the other pages.

What else have I missed?
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

I thought I'd fixed this by adding the database values into a session array, so replacing:
Code: [ Select ]
$_SESSION['query_row'] = $row;

with
Code: [ Select ]
$_SESSION['query_row'][] = array('phone_number'=> $row['phone_number'],'alt_phone_wording'=>$row['alt_phone_wording'],'affiliate_name' => $row['affiliate_name'],'logo_source' => $row['logo_source'],'logo_alt' => $row['logo_alt']);


But this isn't working either and I feel I'm digging a big hole - help!
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Remember to use session_start() on ALL pages you want to use sessions on.
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

session_start() is at the top of the script (see first post). This is in an include file on every page. Do I need to add it again?
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

OK, I think I'm slowly getting somewhere.

I've removed the whole script (from first post) and replaced it with session_start() on the second page. So the long script is only on the landing/first page.

This works in so far as the variables are being stored in the session and displayed on the second page. The only thing it doesn't do is keep the variables if they browse back to the original page or any other landing page as it tries to execute the script again. If anyone knows of way to fix this, it would be much appreciated.

Perhaps I need some kind of script to say if the variables haven't been stored, then run the script, but if they have, skip it?

Thanks again for your help so far!
  • caravan5
  • Novice
  • Novice
  • caravan5
  • Posts: 20

Post 3+ Months Ago

Apologies for the continual posts on this, but I think I've fixed it by adding the following at the top of each page so that the script is only executed if the session hasn't already started.

Code: [ Select ]
session_start();
if (empty($_SESSION['affiliate']))
    {
    include '../scripts/affiliate-details.php';
    }
if (!empty($_SESSION['affiliate']))
    {
    $linkSuffix=(is_array($_SESSION['affiliate']) ? http_build_query($_SESSION['affiliate'], '', '&amp;') : '');
    }    
  1. session_start();
  2. if (empty($_SESSION['affiliate']))
  3.     {
  4.     include '../scripts/affiliate-details.php';
  5.     }
  6. if (!empty($_SESSION['affiliate']))
  7.     {
  8.     $linkSuffix=(is_array($_SESSION['affiliate']) ? http_build_query($_SESSION['affiliate'], '', '&amp;') : '');
  9.     }    


Yey! I may make a programmer yet! :D

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 56 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.