Jump to content

sessions inside functions?


spires

Recommended Posts

Hi Guys.

 

I'm trying to create a session inside a function.

At the top of every page I have <?PHP tracking(); ?>

which calls in the function.

 

What should happen is on the first page that the user

comes to (From Adwords) will place the keyword in to

a session (page?kw=keyword). Then if the $_GET[kw] is not

present, it will display the session.

 

How ever, it's not working. Below is the code i'm trying to

get to work. Please could you advice what I need to do.

 

Thanks

 

This is the function.

<?PHP
function tracking(){
session_start();

$get_Keyword = $_GET['kw'];

if (get_Keyword){
$_SESSION['key'] = $get_Keyword;
echo $_SESSION['key'];
}else{
echo $_SESSION['key'];
}

}
?>

Link to comment
https://forums.phpfreaks.com/topic/90533-sessions-inside-functions/
Share on other sites

you are missing a $ in front of your variable in your IF statement, here is a slightly cleaner version of the code too

 

<?php
function tracking(){
  session_start();
  if ($_GET['kw'])
    $_SESSION['key'] = $_GET['kw'];
  echo $_SESSION['key'];
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.