Home N-13 News Forums Help Search
RegisterForgot password?
How to add image to post necklacesdiscou
Known bugs - 4.0.3 necklacesdiscou

Latest N-13 News 4.0.3

What is N-13 News?
Where can I get help?
Known bugs

Forums Tutorials Simple user login system
1 2
Chris
Posted on 29 Jun 2006, 02:15:38

Access: Admin
Total Posts: 1395
Joined: 2006-05-19

Code:
<?php
session_start
();
$name[] = "Chris"; $pass[] = "123456";
$name[] = "Test"; $pass[] = "weewee";
$name[] = "blah"; $pass[] = "moo";
#create an array of usernames and passwords
#add as many as you want

function loginform(){ #this is the form the user sees if they are not logged in
echo '<fieldset style="padding: 2">';
echo 
'<legend>Login</legend>';
echo 
'<form method=post action="?">';
echo 
'Name: <input type=text name=username value="' . $_POST[username] . '">';
echo 
'<br>';
echo 
'Pass: <input type=password name=pass>';
echo 
'<br>';
echo 
'<input type=submit name="s1" value="Login">';
echo 
'</form>';
echo 
'</fieldset>';
}

#used for the logout link, changes the session 'loggedin' = false if ?action=logout
if($_GET['action'] == "logout"){
  
$_SESSION['loggedin'] = false;
  
$_SESSION['username'] = "";
}

if(
$_SESSION['loggedin'] == false){ #checks if the user isnt logged in
   
if(!$_POST['s1']){     #checks if the form has been submitted, if not show it
     
loginform();
   }else{
     
#username check
     
$tmpname = $_POST['username']; #the username the user has submitted
     
$tmppass = $_POST['pass']; #the password the user has submitted

     
$t = count($name); #count the total users
     
$i = 0;
#create a loop to go through each username/password and compare it with the name pass which was submitted
     
while($i <= $t){
        if(
$tmpname == $name[$i] && $tmppass == $pass[$i]){
        
#if the name/pass submitted matches any of the username and passwords set the session 'loggedin' = true
          
$_SESSION['loggedin'] = true;
          
$_SESSION['username'] = $name[$i];
          
header('Location: ?'); #reload the page
        
}
      
$i++;
     }
    echo 
"invalid name and password :o"; #show an error message if the user enters an incorrect name/pass
    
loginform(); #show the form
   
}
die;
}
?>

Welcome <?php echo $_SESSION['username']; ?>. anything put here is only visible to users who are logged in
<br>
<a href="?action=logout">Logout</a>
#3
Last edited by Chris at 2013-06-17 12:49:14 Reason:
Chronocalamity
Posted on 05 Jan 2008, 18:14:34

Access: Member
Total Posts: 6
Joined: 2008-01-05

ive been looking at this script of yours for like the last 3 hours trying to figer out how to use it to protect a few of my directorys on my site just so a few friends can use those directorys only

I thought you might have some insight on this plus i guess it would be good for you to know whats happening with your stuff

I made something that you might call successful but i think it can be better

by putting the code below in ever page that follows the login script ive been able to keep out anyone unwelcomed "hypothetically" its not been fully tested yet

Code:
<?php 
 session_start
();
 if ((
$_SESSION[loggedin])== true){ 
 echo 
"You are logged in";}
 
 else {
header('Location: ../login.php');
 }
?>


and in your script i change the location for if you log on it sends you to the page that has that in it

Code:
<?php 
          $_SESSION
['loggedin'] = true;
          
$_SESSION['username'] = $name[$i];
          
header('Location: /download1/index.php'); #reload the page
?>


Im new to php i started like 2 weeks ago but it seems i pick it up faster then anyone else i know and understand a good deal of it already

if you have a better way of what im trying to do please give me some pointers
and thx for the sample script it helped me out with learning about sessions
#141
Chris
Posted on 06 Jan 2008, 02:44:47

Access: Admin
Total Posts: 1395
Joined: 2006-05-19

A similar method would be to save my code in a file called 'config.php', then in all the pages you want to protect add this

Code:
<?php 
include 'config.php';
?>


That way each time one of the files load, it loads the config file, the config file then checks if the user is logged in or not.

if your having any problems feel free to add me on MSN, I'm usually always on there. chris@network-13.com
#142
Last edited by Chris at 2008-01-06 02:45:09 Reason:
Chronocalamity
Posted on 06 Jan 2008, 03:40:44

Access: Member
Total Posts: 6
Joined: 2008-01-05

Quote:
I didnt think about that thx for the heads up

...Nextday follow up
Oh wow that works great and just what i needed thx alot
now i can focus on the design part of it and fine tuning
#143
Last edited by Chronocalamity at 2008-01-06 23:06:18 Reason:
Chris
Posted on 08 Jan 2008, 14:05:32

Access: Admin
Total Posts: 1395
Joined: 2006-05-19

Glad I could help :new_cheesy:
#144
Calamity
Posted on 11 Apr 2008, 01:44:54

Access: Member
Total Posts: 33
Joined: 2008-04-11

Hey i've found a glitch where if u leave both fields empty and just click login it logins in.. i was trying to make something that tells it to not log in when it has a blank username and password but i couldn't get it to work... i altered the code and made the username and password field have a value (like in the username field it says username, and in the password field it says password.. well it has 8 dots) and i made it so when u click in the field it replaces it to an empty value.. but that still has problems cause if u click in the username field then the pass... that means it will clear em both out.. then u can click login and u will get through.. so i was wonder if you could add a script into it telling it to reject a blank username and password and have it say invalid username and password...

sry.. i just wanted to point it out... and i would create it myself, but i'm just learning PHP... i'm really good at that... but yea. i'm 15.. and right now i don't hav the time for figuring out alot of php.. cause i gotta keep my grades up.. expecially math.. i'm working on getting into MIT, i finished up alg 2... and i'm in the 9th grade.. and got 100's all year... and i'm taking acc. geomatry and pre calc next year.. and calc in 11th grade and AP calc in 12th.. so i have to keep them up... i have only 1 chance to get in haha...

thx in advance... sry for alittle off topic at the end...
#153
Chris
Posted on 11 Apr 2008, 09:42:51

Access: Admin
Total Posts: 1395
Joined: 2006-05-19

Very well spotted I'm surprised that no one noticed that sooner :eek:

To fix that you just need to add a new check to the while loop, basically tell PHP if the username or password the user has submitted is blank, don't check it against the real name/passwords. To do that edit the while loop as follows

Code:
<?php 
     
while($i <= $t){
         if(
$tmpname !== "" || $tmppass !== ""){
            if(
$tmpname == $name[$i] && $tmppass == $pass[$i]){
            
#if the name/pass submitted matches any of the username and passwords set the session 'loggedin' = true
              
$_SESSION['loggedin'] = true;
              
$_SESSION['username'] = $name[$i];
              
header('Location: ?'); #reload the page
            
}
        }
      
$i++;
     }
?>


The important part is,

Quote:
$tmpname !== "" || $tmppass !== ""

the "!==" means not equal to and the "||" means OR, so if $tmpname OR $tmppass are not blank, continue to check it against the real name/passwords.

Hope this helps you out and thanks for pointing out that error :happy:
#154
Calamity
Posted on 11 Apr 2008, 21:31:17

Access: Member
Total Posts: 33
Joined: 2008-04-11

Thx... i think i know where to place it..... probably not... could u tell me where to stick it just in case i don't find it... thx again for it..
#155
Chris
Posted on 11 Apr 2008, 23:09:08

Access: Admin
Total Posts: 1395
Joined: 2006-05-19

Heres the script with the changes made to it.

Code:
<?php
session_start
();
$name[] = "Chris"; $pass[] = "123456";
$name[] = "Test"; $pass[] = "weewee";
$name[] = "blah"; $pass[] = "moo";
#create an array of usernames and passwords
#add as many as you want

function loginform(){ #this is the form the user sees if they are not logged in
echo '<fieldset style="padding: 2">';
echo 
'<legend>Login</legend>';
echo 
'<form method=post action="?">';
echo 
'Name: <input type=text name=username value="' . $_POST[username] . '">';
echo 
'<br>';
echo 
'Pass: <input type=password name=pass>';
echo 
'<br>';
echo 
'<input type=submit name="s1" value="Login">';
echo 
'</form>';
echo 
'</fieldset>';
}

#used for the logout link, changes the session 'loggedin' = false if ?action=logout
if($_GET['action'] == "logout"){
  
$_SESSION['loggedin'] = false;
  
$_SESSION['username'] = "";
}

if(
$_SESSION['loggedin'] == false){ #checks if the user isnt logged in
   
if(!$_POST['s1']){     #checks if the form has been submitted, if not show it
     
loginform();
   }else{
     
#username check
     
$tmpname = $_POST['username']; #the username the user has submitted
     
$tmppass = $_POST['pass']; #the password the user has submitted

     
$t = count($name); #count the total users
     
$i = 0;
#create a loop to go through each username/password and compare it with the name pass which was submitted
     
while($i <= $t){
        if(
$tmpname !== "" || $tmppass !== ""){
            if(
$tmpname == $name[$i] && $tmppass == $pass[$i]){
            
#if the name/pass submitted matches any of the username and passwords set the session 'loggedin' = true
              
$_SESSION['loggedin'] = true;
              
$_SESSION['username'] = $name[$i];
              
header('Location: ?'); #reload the page
            
}
        }
      
$i++;
     }
    echo 
"invalid name and password :o"; #show an error message if the user enters an incorrect name/pass
    
loginform(); #show the form
   
}
die;
}
?>

Welcome <?php echo $_SESSION['username']; ?>. anything put here is only visible to users who are logged in
<br>
<a href="?action=logout">Logout</a>
#156
Calamity
Posted on 12 Apr 2008, 03:02:49

Access: Member
Total Posts: 33
Joined: 2008-04-11

lol thx.. i figured it out right away though... i also altered the code some bit... i took out the login part... and created my own login script thing, and have the script you created and added it so it redirects to the login page... and then i include the script to the page i want protected, and then just have the login thing go to the page i used the include for.. and works like a charm....

here is what i made it... hope you don't care that i altered it (but it works great)


Code:
<?php 
session_start
();
$name[] = "admin"; $pass[] = "blah";
$name[] = "blah"; $pass[] = "blah";
$name[] = "blah"; $pass[] = "blah";
$name[] = "blah"; $pass[] = "blah";
#create an array of usernames and passwords
#add as many as you want


#used for the logout link, changes the session 'loggedin' = false if ?action=logout
if($_GET['action'] == "logout"){
  
$_SESSION['loggedin'] = false;
  
$_SESSION['username'] = "";
}

if ((
$_SESSION[loggedin])== false){ 
   if(!
$_POST['s1']){  
      
header('Location: ../admin-login.php');
  }else{
     
#username check
     
$tmpname = $_POST['username']; #the username the user has submitted
     
$tmppass = $_POST['pass']; #the password the user has submitted

     
$t = count($name); #count the total users
     
$i = 0;
#create a loop to go through each username/password and compare it with the name pass which was submitted
     
while($i <= $t){
         if(
$tmpname !== "" || $tmppass !== ""){
            if(
$tmpname == $name[$i] && $tmppass == $pass[$i]){
            
#if the name/pass submitted matches any of the username and passwords set the session 'loggedin' = true
              
$_SESSION['loggedin'] = true;
              
$_SESSION['username'] = $name[$i];
              
header('Location: ?'); #reload the page
            
}
        }
      
$i++;
     }
    echo 
"Invalid Username or Password 0.o";#show an error message if the user enters an incorrect name/pass
   
}
die;
}
?>

Welcome <?php echo $_SESSION['username']; ?>.<br>
<a href="?action=logout">Logout</a><br><br>


oh and sry.. but off topic... the php news system... i'm having trouble with the time... i put in my timezone -5 (USA EASTERN TIME) and its still wrong.. what do i have to do.. i looked in the index.php file and just got confused.. i didn't want to mess something up =(
#157
1 2
Network-13.com © 2013