Logging in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Logging in

Post by ace2600 »

I want a user to login to my site and then stay logged into my site as he goes to different pages in it. Heres some code I copied from another post.
Login Form

Code: Select all

<form action="page.php" method="post"> 
Username: <input type="text" name="username"><br> 
Password: <input type="password" name="password"><br> 
<input type="submit" value="Login">
Proccess Login

Code: Select all

if(mysql_num_rows(mysql_query("SELECT * FROM login_table WHERE username='".$_POST&#1111;'username']."' AND password = '".$_POST&#1111;'password']."'")) == 1) &#123; 
session_start(); 
$_SESSION&#1111;'LOGGED_IN'] = 1; 
echo "You are now logged in"; 
&#125; else &#123; 
echo "Invalid username or password"; 
&#125;
Check if someone is logged in

Code: Select all

if($_SESSION&#1111;'LOGGED_IN']==1) &#123; 
echo "You are logged in"; 
&#125; else &#123; 
echo "Please log in"; 
&#125;
I am a newbie at this. Can some please explain the code to me, I understand the form part, but I have no clue about the session_start(), the $_SESSION['LOGGED_IN'], the $_POST['password'], or how it knows that if user bob and user jane are both logged in, to get user bob his information and user jane her information and not switch them.
Thank you!
-Ace
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

I can help....be confused!

The page you login from:

Code: Select all

<?php
session_start();

mysql_connect("localhost:3306", "poet") && mysql_select_db("poetry")
or $failed = "Could not connect to database.";

$listquery = "select * from users where type='o' order by rand() limit 1";
$listresult = mysql_query($listquery);
while ($row = mysql_fetch_assoc($listresult)) 
	$randauthor=$row&#1111;"usernum"];


$listquery = "select * from poems where type='o' order by rand() limit 1";
$listresult = mysql_query($listquery);
while ($row = mysql_fetch_assoc($listresult)) 
	$randnum=$row&#1111;"num"];
?>

<html>
<head>
<title>poetry.mine.nu</title>
<link rel='stylesheet' type='text/css' href='default.css' />
</head>
<body marginheight='0' marginwidth='0' topmargin='0' leftmargin='0'>
  <table cellpadding='0' cellspacing='4' class='MAINTBL'>
    <tr>
      <td class='HEADER' colspan='2'><b>v. 3.9</b></td>
    </tr>
    <tr>
      <td class='VALIGN'>
        <table cellpadding='0' cellspacing='0' class='EXPANDTBL'>
        <tr>
        <td>
        <?php
          if(session_is_registered("usernum"))
          &#123;
                  echo "<table cellpadding='0' cellspacing='0' class='LOGINTBL'>
                          <tr>
                            <td>
     	                      <b>You are logged in as $loggedin.</b><br /><form action='logout.php' method='post'><img src='keys.gif' /> <input type='image' src='logout.gif' class='BUTTON' value='Logout' /></form>
                              </td>
                          </tr>
                        </table>";
          &#125;
          else
          &#123;
	
                  echo "<table cellpadding='0' cellspacing='0' class='LOGINTBL'>
                          <tr>
                            <td>
                              <form action='login.php' method='post'>
                              Username:
                            </td>
                            <td><input type='text' name='un' size='14' maxlength='12' /></td>
                          </tr>
                          <tr>
                            <td>Password:</td>
                            <td><input type='password' name='pw' size='14' maxlength='12'/></td>
                          </tr>
                          <tr>
                            <td class='LOGBUTTON' colspan='2'><input type='image' src='login.gif' class='BUTTON' value='Login' /></form></td>
                          </tr>
                          <tr>
                            <td class='CENTER'><a href='terms.php'>Sign-Up</a></td>
                            <td class='CENTER'><a href='forgotpw.php'>Forgot Password</a></td>
                          </tr>
                        </table>";
          &#125;
        ?>
      </td>
    </tr>
    <tr>
      <td>
        <table cellpadding='0' cellspacing='0' class='MENUTBL1'>
          <tr>
            <td class='SUBMENUTBL2'>
              <table cellpadding='0' cellspacing='0' class='MENUTBL2'>
                <tr>
                  <td></td>
                </tr>
              </table>
            </td>
            <td class='SPACE'><td>
            <td>
              <table cellpadding='0' cellspacing='0' class='MENUTBL3'>
                <tr>
                  <td>
                    <a href='index.php' class='MENU'>Home</a><br />
                    <a href='info.php' class='MENU'>Information</a><br />
                    <a href='poetry.php' class='MENU'>The Poetry</a><br />
                    <a href='users.php' class='MENU'>The Poets</a><br />
                    
                    <?php
                      echo "<a href='usrpoems.php?author=$randauthor' class='MENU'>Random Journal</a><br />";
                      echo "<a href='read.php?num=$randnum' class='MENU'>Random Poem</a><br />";
                    ?>
                    
                    <a href='contact.php' class='MENU'>Contact Us</a><br /><br />
                    <a href='http://www.teenopendiary.com/' class='MENU'>Teen Open Diary</a><br />
                    <a href='http://www.poetry.com/' class='MENU'>Poetry.com</a>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <?php
    if(session_is_registered('usernum'))
    &#123;
            echo "<tr>
                    <td>
                      <table cellpadding='0' cellspacing='0' class='MENUTBL1'>
                        <tr>
                          <td class='SUBMENUTBL2'>
                            <table cellpadding='0' cellspacing='0' class='MENUTBL2'>
                              <tr>
                                <td></td>
                              </tr>
                            </table>
                          </td>
                          <td class='SPACE'><td>
                          <td>
                            <table cellpadding='0' cellspacing='0' class='MENUTBL3B'>
                              <tr>
                                <td>
                                  <a href='usrpoems.php?author=$usernum' class='MENU'>My Journal</a><br />
                                  <a href='newpoem.php' class='MENU'>New Poem</a><br />
                                  <a href='account.php' class='MENU'>My Account</a><br /><br />
				  <a href='allnotes.php?author=$usernum' class='MENU'>All Notes</a><br />
				  <a href='dljournal.php?author=$usernum' class='MENU'>Download Journal</a><br />
				  <a href='deletej.php' class='MENU'>Delete Journal</a>
                                </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                      </table>
                    </td>";
    &#125;
  ?>
          </td>
          </tr>
	  <tr>
	    <td class='VEXPAND'></td>
	  </tr>
          </table>
	</td>
	<td rowspan='2' class='MAINTXT'>
The page that does the logging in:

Code: Select all

<?php
session_start();


mysql_connect("localhost:3306", "poet") && mysql_select_db("poetry")
or $failed = "Could not connect to database.";



$result = mysql_query("select * from users where un='$un'");
$row = mysql_fetch_assoc($result);

if(session_is_registered("loggedin"))
&#123;
	$welcome = "<table cellpadding='0' cellspacing='0' class='ERROR' align='center'><tr><td>You're already logged in, silly.</b> <img src='silly.gif' /></td></tr></table>";
&#125;
else if($row&#1111;"pw"]==md5($pw))
&#123;
	$loggedin = $row&#1111;"un"];
	session_register("loggedin");
	$fname = $row&#1111;"fname"];
	$usernum = $row&#1111;"usernum"];
	session_register("usernum");
	$welcome = "Hello, $fname. Please navigate your account by using the links in the bottom left menu of this page.";
&#125;
else
&#123;
        $welcome = "<table cellpadding='0' cellspacing='0' class='ERROR' align='center'><tr><td>Sorry, the username or password you entered was incorrect, please try again.</td></tr></table>";
&#125;

include "tprint.php";

echo $welcome;

include "bprint.php";

$prevurl = "login.php";
session_register("prevurl");
?>
The logout page:

Code: Select all

<?php
session_start();

mysql_connect("localhost:3306", "poet") && mysql_select_db("poetry")
or $failed = "Could not connect to database.";





if(session_is_registered("usernum"))
&#123;
	$loggedin = "";
	$usernum = "";
	session_unregister("loggedin");
	session_unregister("usernum");
	session_unregister("option2");
	$logout = "<b>You have successfully logged out! Good day.</b>";
&#125;
else
&#123;
	$logout = "<table cellpadding='0' cellspacing='0' class='ERROR' align='center'><tr><td>You must be logged in to log out, silly.</b> <img src="silly.gif" /></td></tr></table>";
&#125;

include("tprint.php");

echo $logout;

include("bprint.php");

$prevurl = "logout.php";
session_register("prevurl");
?>
etc. etc.....if ur smart u can figure it out from there....hope i helped!
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I'm kinda confused.... :lol:
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Post by ace2600 »

Never mind what was originally posted here, I found the error in my ways (plus its a problem one of the top 3 post explains)
-Ace
Post Reply