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 12 of 12
  1. #1
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts

    if statement (Solved)

    Hi!

    I'm using a login script based no
    http://php.about.com/od/finishedphp1...login_code.htm

    I want to know what to use for the conditional statement so i can do something like:

    PHP Code:
    <?php
    if(USER IS LOGGED IN) {
    echo 
    "youre logged in";
    } else {
    ?>
    <form name="login" method="POST" action="../login.php">
        <table border="0">
    <tr><td><font size=1>User:</font></td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td><font size=1>Pass:</font></td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php

    ?>
    any help?
    I just need to know what to put to make the condition.
    Last edited by Zangeel; 01-20-2008 at 11:31 AM.

  • #2
    New Coder
    Join Date
    Oct 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well you need to check to see if the submit button has been pressed.

    Code:
    <?php
    
    if(isset($_POST['Submit'])) {
    echo "youre logged in";
    } else {
    ?>
    <form name="login" method="POST" action="../login.php">
        <table border="0">
    <tr><td><font size=1>User:</font></td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td><font size=1>Pass:</font></td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php
    } 
    ?>
    Hope that helps
    Last edited by adze; 01-19-2008 at 11:45 AM. Reason: code

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I think the answer is there in the same site.
    http://php.about.com/od/finishedphp1...gin_code_6.htm
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    not really.
    the member page checks for a cookie and if you're not logged in it simply redirects you, not exactly what im looking for.

    and in case i wasnt to clear what i want is

    theres a column on my page, it says log in, and has the log in form.
    doesn't make much sense to have the login form if you're already logged in.
    so i want an else there so i can remove it and put a message instead

  • #5
    New Coder
    Join Date
    Oct 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    well like i said above, this should fix your problem!!

    Code:
    <?php
    
    if(isset($_POST['Submit'])) {
    echo "youre logged in";
    } else {
    ?>
    <form name="login" method="POST" action="../login.php">
        <table border="0">
    <tr><td><font size=1>User:</font></td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td><font size=1>Pass:</font></td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php
    } 
    ?>

  • #6
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post
    i think the solution is
    PHP Code:
    <?php
    if(USER IS LOGGED IN) {
    echo 
    "youre logged in";
    } else {
    echo 
    "your html code to login";
    }
    ?>
    in your html code change the double quotes in single quotes or just let them away

  • #7
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Quote Originally Posted by adze View Post
    well like i said above, this should fix your problem!!

    Code:
    <?php
    
    if(isset($_POST['Submit'])) {
    echo "youre logged in";
    } else {
    ?>
    <form name="login" method="POST" action="../login.php">
        <table border="0">
    <tr><td><font size=1>User:</font></td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td><font size=1>Pass:</font></td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php
    } 
    ?>

    No CiGar, tested. --fFfailed---

  • #8
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    There was a small error with the code, try this:

    PHP Code:
    <?php

    if(isset($_POST['submit'])) {
    echo 
    "youre logged in";
    } else {
    ?>
    <form name="login" method="POST" action="../login.php">
        <table border="0">
    <tr><td><font size=1>User:</font></td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td><font size=1>Pass:</font></td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php

    ?>

  • #9
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    You need to have some way to check if your user is logged in. If you just say

    PHP Code:
    if(isset($_POST['submit']) {
    #logged in code
    }
    else {
    #not logged in code

    It will show #logged in code just one page after user hits submit button.

    That's why cookies are used, beacouse in cookies or php sessions you store data on users computer.


    Then when user closes browser (if he doesn't delete cookies of course) when he get's back on your blog he will be logged in.

    Try php.net/predefined_variables


    Here's how i tried using sessions for registration website

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    session_start();

    if (isset(
    $_POST['registration'])):
        print 
    "Do user check function...";
        
        
    $to            =    "matak@mail.com"//
        
    $from        =    "";
        
    $ime        =    $_POST['nickname'];
        
    $subject     =    "**** registration";
        
    $sadrzaj    =    "Mail šalje: " $ime ;
        
    $header        =    "From: $from"
        
    mail($to,$subject,$sadrzaj,$header) or die("PHP ERRORRR");

    elseif (!isset(
    $_POST['check'])):
        print 
    file_get_contents('html/rules.html');
        
    $_SESSION['session_on'] = 1;
    elseif(
    $_POST['check'] == "on" && empty($_SESSION['session_on'])):
        print 
    file_get_contents("html/cookies.html");
    elseif(
    $_POST['check'] == "on" && $_SESSION['session_on'] == TRUE):
        print 
    file_get_contents("html/registration.html");
    endif;

    ?>
    Last edited by matak; 01-19-2008 at 11:07 PM. Reason: removed email :eek:

  • #10
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Not working guys.

    Let me show you exactly what im working with here,

    Step 1 I added the sql
    PHP Code:
    CREATE TABLE users (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEYusername VARCHAR(60), password VARCHAR(60)) 
    Step 2 i created registration

    PHP Code:
    //This code runs if the form has been submitted
    if (isset($_POST['submit'])) {

    //This makes sure they did not leave any fields blank
    if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
    die('You did not complete all of the required fields');
    }

    // checks if the username is in use
    if (!get_magic_quotes_gpc()) {
    $_POST['username'] = addslashes($_POST['username']);
    }
    $usercheck = $_POST['username'];
    $check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'")
    or die(mysql_error());
    $check2 = mysql_num_rows($check);

    //if the name exists it gives an error
    if ($check2 != 0) {
    die('Sorry, the username '.$_POST['username'].' is already in use.');
    }

    // this makes sure both passwords entered match
    if ($_POST['pass'] != $_POST['pass2']) {
    die('Your passwords did not match.');
    }

    // here we encrypt the password and add slashes if needed
    $_POST['pass'] = md5($_POST['pass']);
    if (!get_magic_quotes_gpc()) {
    $_POST['pass'] = addslashes($_POST['pass']);
    $_POST['username'] = addslashes($_POST['username']);
    }

    // now we insert it into the database
    $insert = "INSERT INTO users (username, password)
    VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
    $add_member = mysql_query($insert);
    ?>


    <h1>Registered</h1>
    <p>Thank you, you have registered - you may now login</a>.</p><?php
    }
    else
    {
    ?>


    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table border="0">
    <tr><td>Username:</td><td>
    <input type="text" name="username" maxlength="60">
    </td></tr>
    <tr><td>Password:</td><td>
    <input type="password" name="pass" maxlength="10">
    </td></tr>
    <tr><td>Confirm Password:</td><td>
    <input type="password" name="pass2" maxlength="10">
    </td></tr>
    <tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
    </form>

    <?php
    }
    ?>
    Step 3 login
    PHP Code:
    <?php
    // Connects to your Database
    mysql_connect("your.hostaddress.com""username""password") or die(mysql_error());
    mysql_select_db("Database_Name") or die(mysql_error());

    //Checks if there is a login cookie
    if(isset($_COOKIE['ID_my_site']))

    //if there is, it logs you in and directes you to the members page
    {
    $username $_COOKIE['ID_my_site'];
    $pass $_COOKIE['Key_my_site'];
    $check mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
    while(
    $info mysql_fetch_array$check ))
    {
    if (
    $pass != $info['password'])
    {
    }
    else
    {
    header("Location: members.php");

    }
    }
    }

    //if the login form is submitted
    if (isset($_POST['submit'])) { // if form has been submitted

    // makes sure they filled it in
    if(!$_POST['username'] | !$_POST['pass']) {
    die(
    'You did not fill in a required field.');
    }
    // checks it against the database

    if (!get_magic_quotes_gpc()) {
    $_POST['email'] = addslashes($_POST['email']);
    }
    $check mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

    //Gives error if user dosen't exist
    $check2 mysql_num_rows($check);
    if (
    $check2 == 0) {
    die(
    'That user does not exist in our database.<a href=add.php>Click Here to Register</a>');
    }
    while(
    $info mysql_fetch_array$check ))
    {
    $_POST['pass'] = stripslashes($_POST['pass']);
    $info['password'] = stripslashes($info['password']);
    $_POST['pass'] = md5($_POST['pass']);

    //gives error if the password is wrong
    if ($_POST['pass'] != $info['password']) {
    die(
    'Incorrect password, please try again.');
    }else
    {

    // if login is ok then we add a cookie
    $_POST['username'] = stripslashes($_POST['username']);
    $hour time() + 3600;
    setcookie(ID_my_site$_POST['username'], $hour);
    setcookie(Key_my_site$_POST['pass'], $hour);

    //then redirect them to the members area
    header("Location: members.php");
    }
    }
    }
    else
    {

    // if they are not logged in
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
    <table border="0">
    <tr><td colspan=2><h1>Login</h1></td></tr>
    <tr><td>Username:</td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td>Password:</td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php
    }

    ?>
    Then other irrelevant things.
    But then I wanted to add a login form on the header of my site.
    So I used this..

    PHP Code:
    <p style="margin-top: 0; margin-bottom: 0" align="center"><font color=red><font size=1>Login, or <a href=/register.php>Register</a><font color=black><br><br>
    <?php

    if(isset($_POST['submit'])) {
    echo 
    "youre logged in";
    } else {
    ?>
    <form name="login" method="POST" action="../login.php">
        <table border="0">
    <tr><td><font size=1>User:</font></td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td><font size=1>Pass:</font></td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php

    ?> 
    </p>
    yet, it's not working ;s

  • #11
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    you should check for COOKIE not for POST..

  • Users who have thanked matak for this post:

    Zangeel (01-20-2008)

  • #12
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts

    solved

    =SOLVED=

    Solution:
    PHP Code:
    <?php

    if(isset($_COOKIE['ID_my_site'])) {         
    echo 
    "youre logged in";
    } else {
    ?>
    <form name="login" method="POST" action="../login.php">
        <table border="0">
    <tr><td><font size=1>User:</font></td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td><font size=1>Pass:</font></td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php

    ?>
    Champion Work.


  •  

    Posting Permissions

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