Help with PHP members-only pages?
-
Hello all, I am relatively new to PHP and just finished up coding a basic login system with the aid of a very good little tutorial- but now I need to make certain pages visible only to a member once they have successfully logged in. Code: //checklogin.php <?php $host="hostname"; $username="username"; $password="password"; $db_name="daname"; $tbl_name="tablename"; //I left out my actual host and what not on purpose- the information is filled in on the actual code mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> //main_login.php <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="password" id="mypassword"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table></P> //login_success.php <? session_start(); if(!session_is_registered(myusername))… header("location:main_login.php"); } ?> //this is at the top of a css/html page for the successful login //logout.php <? session_start(); session_destroy(); ?> //this is at the top of a css/html page for the successful logout I don't know if there is a problem with the php I have already coded, but I have tried several different methods and have been unable to successfully make a members-only page. Help is much appreciated! ~The Paper Dragon
-
Answer:
on the login page if the login info is correct: $_SESSION['logged']=true; and on your member page: if($_SESSION['logged']){ // Show page }else{ // Error } or if(isset($_SESSION['logged'])){ }else{ } --- Session register is rather old, http://php.net/manual/en/function.session-is-registered.php
Dragon at Yahoo! Answers Visit the source
Related Q & A:
- How to store data in php and get data from php?Best solution by Stack Overflow
- How do you view public profile of yahoo members?Best solution by Yahoo! Answers
- How many members are allowed in a Yahoo group?Best solution by answers.yahoo.com
- Will Yahoo improve to allow list members to pick a new owner, when an owner leaves the list?Best solution by Yahoo! Answers
- How to create dynamic php pages?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.