![]() |
Creating a random number once and keeping it
Hi,
I'm just beginning to learn PHP (day 2) and I can't really seem to figure this one out. My idea is a simple number guessing game that works something like this: $num = rand(0,100); (Accept $guess from user input) if $num < $guess echo 'too low' etc. The form references itself and sends the guess via POST. My problem is that the random number is getting set each time. I come from a forms/console development background and this is the first time I've delved into web programming, so there's some element here regarding the 'session' that isn't quite straight in my head. Please feel free to enlighten me, poke fun at my code, etc.! Thanks for your help. This is my code. It's almost entirely lifted from "Sams Teach Yourself PHP, MySQL and Apache" but I've modified the functionality to attempt to use a random number. Code:
<?php |
Well, I've fixed it by using a hidden input and passing the value along via POST. Here's the working code:
Code:
<?php |
The thing about web development is that it is stateless due to the nature of http requests. Meaning that you can't save state between requests (for example when you submit a form or click on a link). There is a few things you can do here to save the random number between requests, you can use a cookie to save it and then obtain it later, another would be using sessions (which sometimes are implemented using cookies). The cookies are saved on the client while the sessions are most of the time saved in client and sometimes in server. But on the server side you can also save it in a database or a local file.
Or you can also use the hidden input tag to keep it in the between requests. The next is an example of how it would look using the hidden input. Just gotta make sure the PHP Code:
|
Even though it's correct which you ought to educate oneself in affiliate advertising, the best sources Karen Millen Dresses are not programs. However, books are.Karen Millen Coats
Karen Millen One Shoulder Dress By basically studying up on affiliate advertising, you'll be able to get fantastic insights and preserve lots of funds. By simply looking on retail sites such as Amazon or EBay, it is possible to get hundred of titles on the subject.Karen Millen Solid Color Dresses You can find lots of step-by-step guides obtainable on the way to get your affiliate advertising and marketing business going and the way to develop it up. One more great useful resource for affiliate advertising may be the web. Merely by typing affiliate advertising and marketing into any major online search engine, lots of info arrives up. You will find lots of free classes online that you could take around the subject in the event you are established. Read by means of the information readily available for you for absolutely nothing.Karen Millen Multicolor Dresses Karen Millen Skirts Chances really are a course or a seminar will not educate you anymore than what you are able to learn on the web at no cost. |
| All times are GMT. The time now is 11:54 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0