It is currently Sun Apr 28, 2024 5:11 am



Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
global variables and sessions... 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post global variables and sessions...
ok, I thought I had this fixed, but I was wrong. :/ I can't get this damn thing to work, and it's pissing me off.

Ok, on my first page, I have two links. Both point to index2.php, but one has index2.php?flash=1, thereby setting $flash = 1. Easy enough.

When it gets to the second page, it has no trouble reading $flash as a variable. However, if I do a session_start(), suddenly the variable returns as 0. Doing a session_start() on the referring page doesn't seem to make any difference.

So, to kinda get around that, I did this:

Quote:
if ($flash == 1) {
session_start();
$_SESSION["flash"] = 1;

}
else {
session_start();
$_SESSION["flash"] = 0;
}


and it works. Sorta. Unless someone goes back to that page. At which point $flash is undefined, which kills $_SESSION["flash"].

First, what's the command to extract session variables so that a page can use it (for instance, $_SESSION["flash"] to just $flash).

Alternately, can someone think of a better way to do this? I prefer not to use a form to set the $flash variable, just a link, which is probably where my issues are coming from. :/

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sun Nov 02, 2003 1:54 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
heh...ok, I got it working.

Code:
if ($flash == 1) {
          session_start();
          $_SESSION["flash"] = 1;
         
        }
   else {
          session_start();
}


duh. Anyway, now that I'm not screwing up the global variable, I'm still curious if there's a better way to accomplish this.

I'm also curious why a session_start(); at the beginning of everything wipes out the url-carried variable.

Comments?

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sun Nov 02, 2003 2:11 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
I have not experienced that myself. Can you explain a little more thoroughly what you are experiencing?


Sun Nov 02, 2003 11:40 am
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
erm, sure. I guess you mean about the sesssion_start() and stuff, right? Anyway...

here's my index.php (the only part that matters);

<A href="index2.php?flash=1">Flash version</a>&<a href="index2.php">NonFlash Version</a>

So, selecting the first one sets $flash=1, while the second leaves $flash unset.

On the second page, if I do a;

echo $flash;

after clicking the first link, I get a 1. However, if I do this;

session_start();
echo $flash;

I get nothing. Somehow doing the session_start(); wiped out the $flash variable. So instead I was forced to used an if/else statement which starts the session and manually assigns $_SESSION["flash"] instead of it being automatic like it should be.

I have tried putting a session_start(); in index.php, but it didn't make any difference.

Ermm...if you want, I can post up the actual pages. What I'm trying to accomplish is, of course, a bit more complex than a simple echo. Anyway, lemme know.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sun Nov 02, 2003 1:19 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Try echoing $_GET['flash'] instead of just $flash and see what you get.


Sun Nov 02, 2003 2:02 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
hmmm..ok, I'm really kinda confused as to what precisely was going on, but I think I've (finally) got it.

The $_GET['flash'] does work, but when I was testing it $flash worked also. This is when I call the session_start(); immediately. I was probably doing something wrong before. Bleh.

My biggest problem was when people would refresh the page, or hit index2 from some place other than index (thereby not carrying the variable in the url). I think I've solved that by doing

extract ($_SESSION['flash']);

I tried a bunch of ways to try and confuse it and I've failed to break it so far. :)

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Mon Nov 03, 2003 1:21 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Just don't get into the habit of using global variables, or you will have to retrain yourself later. Don't use extract. Code with $_SESSION, $_GET, etc.


Mon Nov 03, 2003 7:08 am
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
can you pass $_SESSION variables through a URL (GET, I guess)? I couldn't get this to work any other way, but if you have a viable alternative that represents 'good' programming, please, let me know.

That's one of the reasons I'm asking so many questions. I want to groom good programming habits from the start.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Mon Nov 03, 2003 4:47 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
So all you are trying to do track whether they want flash or not? Well, unless you want them to have to click a link EVERY time they come to your site, you either need to use a cookie, or create a database and track their IP. I don't get why you are using sessions for this.

Cookies would be simplest. Just create a simple file to include at the beginning of your files before anything is sent to the browser, and have everything right there. The steps would be pretty simple:
check if cookie is present.
if cookie is then set $flash = 1; or $flash = 2; depending. If there is no cookie, then set $flash = 2; (off) and trigger a form or link asking them to choose what they want.
You can even have the trigger be a simple pop-up window so they don't lose their place.
Then you just set the cookie accordingly.

The option with tracking IP's is not difficult either, and could be done with sessions if you wanted to save a query each page.
Create a table with IP's and a tiny int field to track whether they want flash or not.
On the page, check to see if their is a session var. If there is, then include the flash/no flash file accordingly.
If there is no session, then do a quick query to see if their IP is in the database. If it is, then start a session with the setting from the db. If not, then display a link/form for them to choose.
Once you have their choice, insert a new row into the databaes.

Basically, on their first page the db is checked for their IP. On subsequent pages, the session holds the var and the query is not run.

Something to keep in mind is that people are not always going to enter on your index, and planning your navigation with that as a requirement is a bad idea. You should make your code test for a log in, or in this case a simple var, on every page. If it doesn't exist, prompt them for it.

As far as your issue with passing the variable to every page, I would not advise it. It just doesn't make sense compared to your other options. However, if you want to do it that way, here is how I would code it.
Code:
if( empty($_SESSION['flash']) )
{
     //no session var exists.  check to see if we are creating a session.
     if( empty($_GET['flash']) )
     {
          //no get var exists.  make them tell us what they want.
          include("prompt/for/flash/file.php");  // either include a file, or print a link/form to get their choice
     }
     else
     {
          //they have specified their choice for flash, and now we need to create the session
          //make sure to check to see if they are playing by the rules
          if( $_GET['flash'] != 1 && $_GET['flash'] != 2 )
          {
               echo("NO!  Hommie don't play that!");
          }
          else
          {
               session_start();
               $_SESSION["flash"] = $_GET['flash'];
          }
     }
}

Here is the same code, but cleaned up:
Code:
if( empty($_SESSION['flash']) )
{
     if( empty($_GET['flash']) ) include("prompt/for/flash/file.php");
     else
     {
          if( $_GET['flash'] != 1 && $_GET['flash'] != 2 ) echo("NO!  Hommie don't play that!");
          else
          {
               session_start();
               $_SESSION["flash"] = $_GET['flash'];
          }
     }
}

Feel free to rephrase/reclarify the question.


Mon Nov 03, 2003 6:25 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
hmmm...ok, perhaps setting a cookie would be the best bet. I don't want to track ip addresses in a db... though I wouldn't be doing anything bad with it, I just think tracking like that is wrong. A cookie can at least be deleted. Besides, dynamic ips could potentially cause an issue (though, honestly, not much of one).

I realize most people won't enter through the index. Right now I was just planning on if they did... I planned on having a link under the menus to switch the flash/nonflash versions, with nonflash being default. The way I have it right now, nonflash is the default, and messing with the link was something I was putting off until I got a bunch of other stuff sorted out.

I think I'll be looking into the cookie thing.. if I run into any stumbling blocks, I'll ask some questions.

On a related question, the flash/nonflash link thing... is there a way I could pass the current page as a variable? Basically, if someone clicks the flash/nonflash link, have it forward to another page, set the cookie/variables, then return to the referring page? I know you can get the current page somehow (I forget the function, but I know it exists). I suppose I could just call it on every single page, just in case someone clicks the flash/noflash link, but that seems a bit excessive. hmmm...bleh. That may be the best route, after all. Any thoughts on this?

In the meantime (next time I sit down to code) I'll see if I can figure out the cookie thing.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Mon Nov 03, 2003 7:46 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
2 questions there.

1st:
current page: $_SERVER['PHP_SELF']
previous page: $_SERVER['HTTP_REFERRER']

2nd:
Having a status check on every page is standard design. The best design is not to send your peeps to a different page, and then send them back, but rather include a file that does the checking, and processing.

Let me give you an example. On my site, I have a file that is included on every page before anything else (because of the issue with sending header()'s). This page looks for cookies, and will send cookies if I tell it to.

On "member only" sections of the site, I merely include another file. This second file checks the data gathered in the first file, and checks the db to see if they have appropriate permissions. If they do, the rest of the page displays. If they do not have the cookie, or are not in the database, then the page displays a form for them to log in with, and kills the rest of the page (see die() ).

The form they are given has the action=$_SERVER['PHP_SELF']. The first file then processes the information, sends a cookie, and then sends a header("Location:" . $_SERVER['PHP_SELF']); No matter what page they are at, all the processing is done on that page, and they have never actuallly left it. Make sense? My script is actually more complicated than this, but you get the concept.


Mon Nov 03, 2003 8:02 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
I understand what you're saying with the cookie check/login thing. Though I could do that with the flash thing, I don't want to. Most of my hits are from search engines, and I don't want to force people to go anywhere other than what they were looking for. If they decide to browse further, they can, and if they decide they want the flash version rather than the default html version, they can do that too.

The way I'm conceptualizing it is a link at the bottom of the menu that sends them to flashchoice.php (or maybe nonflashchoice.php as well), which then sets the cookie and immediately redirects them back to the page they came from. I don't want to force any of my visitors to do anything at all before reaching the content they're looking for. Additionally, I don't want to screw up any spiders. :P

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Mon Nov 03, 2003 10:10 pm
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
ok, I got the cookie stuff working, I think. If you don't mind, I'm going to post the code. I'd like any suggestions. It seems to work reliably (after a couple hours of cussing and coding).

(at the top of every page)
Code:
<? include("globals/cookie.php") ?>


(globals/cookie.php)

Code:
<?
/* If cookie doesn't exist, set it */
if (!isset($_COOKIE["clankiller"])){
   if ($flash == 1){
      setcookie ("clankiller", "1", time()+60*60*24*30, "/");
}
   else {
      setcookie ("clankiller", "0", time()+60*60*24*30, "/");
}}
if (isset($flash)){
   
/* If cookie exists and a flash=0 link was clicked, this will reset the cookie to be 0. */

if ($flash == 0){
   setcookie ("clankiller", "0", time()+60*60*24*30, "/");
}

/* If cookie exists and a flash=1 link was clicked, this will reset the cookie to be 1. */

elseif ($flash == 1){
   setcookie ("clankiller", "1", time()+60*60*24*30, "/");
}
}
?>


(this is part of header.txt, which does the javascript, table setup and menu inclusion. I edited out everything but the menu inclusion code)

Code:
<?php
if (isset($_COOKIE["clankiller"]));{
   if (isset($flash)) {
      if ($flash == 1) {
         include ("flashmenu.txt");
      }
      elseif ($flash == 0) {
         include ("nonflashmenu.txt");
      }
   }
elseif ($_COOKIE["clankiller"] == 1) {
   include ("flashmenu.txt");
   }
   
else {
   include ("nonflashmenu.txt");
   }
}
   
?>


(lastly, the link in flashmenu.txt)
Code:
<?
echo ("<A href=$_SERVER[$PHP_SELF]?flash=0>NonFlash</a>");
?>


It seems to work reliably. I had to do a bunch of issets to get around the fact that cookies aren't available until the next page refresh... otherwise you'd click on the nonflash link and it would refresh still as flash... then the NEXT click it would be nonflash. Anyway, that screwed me up a bit. Any comments or better ways of doing stuff?

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue Nov 04, 2003 12:26 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
I'm glad you got it working. Let me show you a couple tricks, and a little more elegant coding.

1st thing is that there is no need to send a cookie if they have not specified anything. If the default HTML instead of flash, then the cookie should not be set unless they specified a preference. Also on this point, the cookie is really only useful if they want flash. Normally you would want to specify the cookie if they chose, regardless of what they chose. In this instance, it is only necessary if they chose to have flash on.

2nd, sounds like you have discovered the pain with cookies. Instead of it being a 2 page process, it becomes a 3 page process. The easy work around to this is to simply send a location header after the sending the cookie, thereby making the extra page transparent to the visitor.

3rd, we all know that 1=on, and 0=off, but don't use it in programming. It will cause you no end of trouble when you start evaluating variables, since 0 can evaulate the same as FALSE. 1=on, 2=off. Also, isset can give you false positives, so stop using it. If you have a form field that is left blank when a form is submitted, then that var is still set, it may be empty, but it is still set. isset() will show the var as being there, even though it is not. Use empty() or strlen() instead. It works fine for you in this instance, but I would get into the habit of using the proper coding habits.

4th, You have only provided a link to one version. Make sure you add a link to the flash menu in the non-flash file. Here is my version of the code (btw, you have an errent semicolon on that first line of code):

drat, time for work. I will post the code when I get there. ciao.


Tue Nov 04, 2003 7:31 am
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
just to get your goat (or pig) before you can reply some more...

1. True, I don't need a cookie if default behaviour is nonflash. However, I can't unset a cookie with an href, so I'd still need the cookie-handling script. So why not just set a cookie as =2 instead of unsetting the cookie? The difference in code is minimal, and I don't think the end user will care either way.

2. I didn't understand this. Could you please elucidate?

3. Ok. I will stop using 0. I will stop using isset(). I'll check out strlen() and emtpy() and see which one I like more.

4. I did. The code is identical with the exception that flash=1. I didn't see the point of posting it since it was exactly the same. Where's the errant semicolon (Mr. Errant Spelling)?

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue Nov 04, 2003 8:07 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 18 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 45 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

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.