Page 1 of 1

using php sessions in an API

Posted: Thu Jan 31, 2008 5:42 pm
by mlecho
is it possible to set and pass session variables through an API? The API in question simpy receives variables by $_GET. I thought perhaps i could set the session variables by way of the $_GET variables in the url. The return of the API is always simple XML. All this is for a flash site i am working with...after a user logs on, we want to be able to utilize that username by way of sessions. However, i am not having much luck passing that variable onward....much less get re-usablity of the variable. Before i blast you with code, is this possible?

Re: using php sessions in an API

Posted: Thu Jan 31, 2008 5:51 pm
by Christopher
$_GET and $_SESSION are two different way to make values available to scripts. You can certainly set session variables from GET values (after proper security meansure ;)) as well as passing values in the session as parameters.

Re: using php sessions in an API

Posted: Thu Jan 31, 2008 6:02 pm
by mlecho
hmmm...so after the initial session variable is set, i should be able to pass it around on multiple queries....i guess i need to check over my code

Re: using php sessions in an API

Posted: Thu Jan 31, 2008 6:24 pm
by Christopher
Once you assign a variable to the session, you do not need to pass it around. That is the whole point of sessions.

Re: using php sessions in an API

Posted: Thu Jan 31, 2008 7:00 pm
by mlecho
k- tail between my legs, let's find out what i don't know about sessions.

Code: Select all

 
<?php
session_start();
session_id('ti');
if(! isset($_SESSION['name']))
{
    if(isset($_GET['name']))
    {
        $_SESSION['name'] = $_GET['name'];
    }else
    {
        $_SESSION['name'] = $_SESSION['name'];
       }
}
 
 
?>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>test</title>
    <meta name="generator" content="BBEdit 8.7">
</head>
<body>
<?php echo $_SESSION['name']; ?>
</body>
</html>
 
so, if i put in website.com?name=myname

i should see myname

and if next i remove the ?name=myname, should it still not still echo myname?