Results 1 to 3 of 3

Thread: how to use cookies to prevent entering a webpage without login

  1. #1
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to use cookies to prevent entering a webpage without login

    if the user has not logged in, but click to the order page, then it should be redirect to the login page.
    yet, it is not functioned as expected. it just hold in the login page, and cannot login or proceed to order page. wt is the problem??


    login check:
    Code:
    <?php
    
    if ($_POST['submit']) {
            //get username and password
           $membername = $_POST['loginID_member'];
           $password = $_POST['password_member'];
    
    	if ($email=="correct" && $pass=="correct"){ 
                setcookie('logged','1');
                        header("Location: order_form.php"); //Redirect to home page
            
      //     else echo "Wrong combinaton!";
    //   }   
    
    	exit();
    	}
    }
    
    
    $email = $_POST['loginID_member'];
    $pass = $_POST['password_member'];
    $_SESSION['email']= $_POST['loginID_member'];
    //$_SESSION['password']= $_POST['password_member'];
    setcookie("email", $email, time()+3600);
    
    if ($email&&$pass) 
    {
    //connect to db
    $connect = mysql_connect("127.0.0.1","root","") or die("not connecting");
    mysql_select_db("fooddeliveryshop") or die("no db :'(");
    $query = mysql_query("SELECT * FROM member WHERE memberemail='$email'");
    $numrows = mysql_num_rows($query);
    
    
    if ($numrows!=0)
    {
    $match = mysql_query("SELECT * FROM member WHERE memberemail='$email' and memberpw='$pass'");
    $numrows2 =  mysql_num_rows($match);
    //echo ($match);
      if ($numrows2!=0)
      {
    	$row = mysql_fetch_assoc($query);
        $dbusername = $row['memberemail'];
        $dbpassword = $row['memberpw'];
    	echo ('Success. Please wait.') ;
    	echo($_SESSION['email']);
    	echo $_COOKIE['email'];
    	if (isset($_COOKIE)) {
        foreach ($_COOKIE as $name => $value) {
            $name = htmlspecialchars($name);
            $value = htmlspecialchars($value);
            echo "$name : $value <br />\n";
    		echo ("YES");
        }}
    	
        header("Location: order_form.php");
    }
         else  die ('<script type="text/javascript">alert("Incorrect username/password!");location.replace("member_login.php")</script>');
    	 
    }
    else
      echo "<script>alert('User does not exist!');window.location.href= 'member_login.php';</script>"; 
    }
    
    
    else
        die('<script type="text/javascript">alert("Please enter a username and password!");location.replace("member_login.php")</script>');
    
    
    //disconnect with db
      mysql_close($connect); 
    ?>
    order page:
    Code:
    <?php 
    session_start();
    
    if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
    
    header ("Location: member_login.php");
    
    }
    ?>

  2. #2
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default

    You're not calling session_start() at the top of your login page.

    *SO, it's not setting the session variable that you're checking in the order_form.php page... it's infinitely going to loop back to the login page until the session var is set.

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    crobinson42's answer should solve your immediate problem.

    If you'd like to make that login script more efficient and user-friendly, I'd be happy to offer some additional suggestions.

Similar Threads

  1. Login Remember Me not setting cookies
    By Kraven1 in forum PHP
    Replies: 1
    Last Post: 11-26-2010, 07:50 PM
  2. ddsmoothmenu javascript error when entering login.aspx
    By alfresco in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 10-22-2010, 07:22 AM
  3. php login script cookies not working
    By SouLz in forum PHP
    Replies: 2
    Last Post: 11-11-2009, 03:28 PM
  4. Replies: 5
    Last Post: 12-16-2008, 11:13 PM
  5. Login Script Using Cookies
    By smithster in forum PHP
    Replies: 1
    Last Post: 05-18-2008, 02:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •