Re: Cookies

From: Date: Tue, 08 Apr 2003 23:12:13 +0000
Subject: Re: Cookies
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-19363@lists.php.net to get a copy of this message
Sessions are just ways for your browser to keep track of things. They disappear when you close the browser. Think of how you can pass variables in PHP via the URL (POST and GET). Here is a login script I use to authenticate against MySQL: //First the HTML. This passes the data, via POST, to authenticate.php: <form method="POST" action="authenticate.php"> <table> <tr> <td> <font class="bodytext"><strong>Username:</strong></font></td> <td><input type="text" name="username" size="20"></td> </tr> <tr> <td> <font class="bodytext"><strong>Password:</strong></font></td> <td><input type="password" name="password" size="20"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="Submit" name="login"> </td> </tr> </table> </form> //Authenticate.php gets the POST info with: $uname=($_POST['username']); $pword=($_POST['password']); //check that the user is calling the page from the login form and not accessing it directly //and redirect back to the login form if necessary if (!isset($uname) || !isset($pword)) { header( "Location: login.php" ); } //check that the form fields are not empty, and redirect back to the login page if they are elseif (empty($uname) || empty($pword)) { header( "Location: login.php" ); } else{ //This data is then altered for use for MySQL: $user = addslashes($uname); $pass = md5($pword); //A MySQL session is started to see if there are any records with that name and password: $dbHost = "localhost"; $dbUser = "user"; $dbPass = "password"; $dbDatabase = "database"; mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database."); @mysql_select_db("$dbDatabase") or die ("Couldn't select the database."); $query="select * from users where username='$user' AND password='$pass'"; $result=mysql_query($query); mysql_close(); //PHP then checks to see if at least one row was returned: $rowCheck = mysql_num_rows($result); if($rowCheck > 0){ while($row = mysql_fetch_array($result)){ //start the session and register a variable: session_start(); //The variable 'valid_user' is now accessible from any page that uses sessions: $_SESSION['valid_user'] = $uname; //Redirects to index.php: header( "Location: index.php" ); } } else { //if nothing is returned by the query, unsuccessful login code goes here: echo 'Incorrect login name or password. Please try again.'; } } //Now, from other pages, you would start them off with: session_start(); if (!isset($_SESSION['valid_user'])){ header( "Location: login.php" ); } else { //your normal page content //you can get the session variable with: $uname=($_SESSION['valid_user']); echo "$uname"; } "Odcs" <odcs@rogers.com> wrote in message news:002e01c2fe1d$4c5ff920$6401a8c0@p1... > I don't understand sessions either. I'm sure they are quite simple - but I'm > just not getting the concept. I have been sitting here for the last 3 hours > trying to figure out a simple login script using sessions, and I'm probably > farther behind than I was when I started. For whatever reason I'm just not > getting the concept of how they work. > > If you can shed some light it would be appreciated. > > B. > > > > ----- Original Message ----- > From: "ODCS" <odcs@rogers.com> > To: <Trystano@aol.com> > Sent: Tuesday, April 08, 2003 3:25 PM > Subject: Re: [PHP-WIN] Cookies > > > > Yes, but I want to know about cookies as they are more secure. > > > > ----- Original Message ----- > > From: <Trystano@aol.com> > > To: <odcs@rogers.com> > > Sent: Tuesday, April 08, 2003 2:44 PM > > Subject: Re: [PHP-WIN] Cookies > > > > > > > Have you heard of Sessions??? > > > > > > You might wanna uses Sessions and session variables to protect your > > pages?? > > > > > > Tryst > > > > > > >

« previous php.windows (#19363) next »