Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    83
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Display sign-in form only once

    How do I display a sign in form for submission just once during a session? It should appear only on the first page the user lands on.

    I intended to use the following approach but it does not seem to work with Opera, although it's fine with all the other mainstream browsers. With Opera only, if you revisit the same page (via a link) or visit a new page after the Form has been accepted, the session variable has gone and the Form is displayed again. It's okay (i.e. the variable persists) if I just refresh the page.

    I have Opera 12.15 on XP with cookies enabled running on XAMPP and localhost. There is no .htaccess.

    Thanks in advance.

    Code:
    <?php
    // Turn on error reporting
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    session_start();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">   
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <title>Opera Session Variable</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    
    <body>
    <?php
    // create a test variable to confirm other session variables outside of Form are   persisting
    $_SESSION['test'] = 'Test';
    
    // function to print session variables
    function print_array( $_SESSION )
    {
    echo '<pre>$_SESSION<br />' . "\n";
    print_r($_SESSION);
    echo "</pre>\n";
    }   
    
    // process the submitted form
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    if (isset($_POST['formaccept'])) {
    $_SESSION['formaccepted'] = $_POST['formaccept'];
    }
    }
    
    // print the session variables
    print_array( $_SESSION );
    
    // only display the form if it has not previously been accepted in this session
    if (!isset($_SESSION['formaccepted'])) {
    ?>
    <p><b>This parargraph should only display if the form has not been accepted in the current session.</b></p>
    <br />
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="submit" name="formaccept" value="Accept" />
    </form>
    <?php
    }
    ?>
    
    </body>
    
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,084
    Thanks
    23
    Thanked 592 Times in 591 Posts
    More that one way to skin this cat, but here is something basic (2 files)
    The first is your index.html - the opening page with just log in and sub for sign up.
    It needs to be validated. Code not here.
    Code:
    <html>
    <head>
    <title>Login</title>
    <!--
    <link rel="stylesheet" type="text/css" href="style.css" />
    -->
    </head>
    <body>
    <div id="containt" align="center">
    <form action="login.php" method="post">
    <div id="header"><h2 class="sansserif">User Login</h2></div>
    <table>
    	<tr>
    		<td>Enter Username:</td>
    		<td> <input type="text" name="username" size="20"></td>
    	</tr>
    	<tr>
    		<td>Enter Password:</td>
    		<td><input type="password" name="password" size="20"></td>
    	</tr>
    	<tr>
    			<td><input type="submit" value="Log In"></td>
    			<td><a href="signup.html">Sign Up</a></td>
    	</tr>
    </table>
    </form>
    </div>
    </body>
    </html>
    The form calls a php file that checks the log in information. I did not sanitize anything on this page because it is not add to the database.
    login.php:
    PHP Code:
     <?php 
    session_start
    ();
    include(
    "config.php");               //including config.php in our file
    $username $_POST["username"];     //Storing username in $username variable.
    $password $_POST["password"];    //Storing password in $password variable.
    $match "select id from $table where username = '".$_POST['username']."' and password = '".$_POST['password']."';"
    $qry mysqli_query($match);
    $num_rows mysqli_num_rows($qry); 
    if (
    $num_rows <= 0) { 
        echo 
    "Sorry, there is no username $username with the specified password.";
        echo 
    "Try again";
        exit; 
    } else {
        
    $_SESSION['user']= $_POST["username"];
        
    header("location:page2.php");      // It is the page where you want to redirect user after login.
    }
    ?>
    I used mysqli and called next site page as page2.php
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    83
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Thanks sunfighter, I'm only using it to get users to accept terms so I don't want it to be too involved.

    I narrowed the problem down to just opera versions 11 and 12 and only on XP. I guess it's a bug.

    Cheers.


  •  

    Tags for this Thread

    Posting Permissions

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