Log in

View Full Version : Arrays--Some Basic Questions



marain
05-05-2012, 02:29 PM
Some array-related questions for which I do not readily find answers in my reference text (McGraw-Hill/Osborne):

I want to define an array containing strings. Whenever a user visits the page, I want to echo a random string from the array. When the visitor refreshes (assume same session, I am not yet advanced enough to consider cookies), I want to echo a different randomly selected string, but exclude from consideration any strings previously echoed that session. I know that I can do this by deleting the array item for any strings that I echo. My first question, though, is this: Does deleting the item remove it from the server for just that user, or is it gone permanently, for everyone? I can also mark the string in some way, so that the script can test for the mark and see whether it's been used. Same question: Does the marking remain when someone else accesses the page?

Next question: If it is gone for good (or if the marking remains), then I imagine I can get around this by copying the entire array and then having the user work with just the copied array. But I can find no internal function to do that copying. Is the only way to copy to loop through the strings in the original array, reading and copying the items into that new array, item by item?

Final question: When multiple users access the page simultaneously, does each user see his or her own "pristine" page, unsullied by the hypothetical deletes that I mentioned above?

Thanks, all, for your availability to educate this neophyte.

A.
Allan Marain

djr33
05-05-2012, 07:37 PM
Hi Allan,

You need to take a step back and look at how the architecture of PHP works, then everything will make sense (and be pretty easy).

A PHP script is just a text file that is saved when you edit it. When you run it, the server reads the contents and executes them. That does NOT change anything in the script. So the next time, it will be the same contents executed again.

A session is an individual bit of data saved on the server. It actually uses a cookie (or possibly other methods-- you don't need to worry about that) to connect the user's computer to the session. They last around 15 minutes (approx.!), after you stop using the connection and/or close the browser session. (This is based usually on the cookie not the session data on the server, but either half missing will make the session end.)

Sessions are completely secure on the server. The only way to access them is the session cookie that the user has. (If that is stolen, the whole session can be used by someone else.) But you, in writing the script, decide what to show the user (and when). So you could in theory store your server's FTP password into a session variable and the user would never see it (unless you intentionally output it for them). Don't do that, but it's a clear example.

Sessions are like tiny databases for each user that just work magically. They're great. Cookies aren't too hard to use either, but sessions are easier, more reliable, and more secure. No point in cookies if you can use sessions. Use cookies only for longer periods of time (like if they return the next day or next week-- although it's not guaranteed they'll still have the cookie, just possible, so it helps but doesn't ensure anything).

Sessions data is not shared between users. And modifying array values while running a script will NOT transfer to other users or to the next time the page is loaded.

In fact, the only trouble you will have is remembering which ones you've shown. But that's easy with sessions-- that's what they're for.

You could do a few things.

Method 1:
1) Store the whole array in the session. 2) Use shuffle() to make the order random. 3) Delete items as you display them. 4) Because you're using $_SESSION it'll be available the next time you load the page.

Method 2:
1) Display items from the permanent array (not in the session). 2) Store the displayed items in the session (by array key is probably easiest). 3) Don't display an item if it is stored as "used" in the session data.



The only way you'd get transfer between users is if you were intentionally storing data in a database or in a text file that you were modifying (reading/writing) dynamically with PHP. You would be aware of this if you were doing it, so don't worry about it. (In the future, of course, you might want to do that.)
Likewise, the same method works for saving data for individual users (preserving sessions) for longer times and creating a login system, if you'd like.


That should get you started! Let me know what aspects are still unclear, if any.

traq
05-05-2012, 08:26 PM
I'd certainly go with Daniel's option #1 :)

<?php
// session_start() MUST go before ANY output to the client!
session_start();

// this is your "starting" array
$myArray = array( 'a','e','i','o','u' );

// check if the particular user has the array in their session
if( empty( $_SESSION['userArray'] ) ){
// if not (or if all elements have been used),
// use the "starting" array to create their session array
$_SESSION['userArray'] = $myArray;
}

// shuffle session array
shuffle( $_SESSION['userArray'] );
// remove and print last element.
print array_pop( $_SESSION['userArray'] );

djr33
05-06-2012, 12:44 AM
Let me add one more detail: that's by far the simplest way to do this and it will work. The only disadvantage is that it requires saving a copy of the whole array. If you have a huge array (thousands of entries) it might be extra work for the server, especially if you have many visitors.

So only if you have a big array and many visitors, you should consider option #2. If it's a small array and not so many visitors, #1 is a good option.

Traq's code should work well!

marain
05-06-2012, 02:17 PM
Guys, your info will keep me busy for quite a while. Thank you, thank you, thank you!

A.