using php sessions in an API

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

using php sessions in an API

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: using php sessions in an API

Post 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.
(#10850)
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Re: using php sessions in an API

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: using php sessions in an API

Post 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.
(#10850)
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Re: using php sessions in an API

Post 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?
Post Reply