This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-01-26 07:39:11

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Need PHP Coders...

How would I track if someone is logged in using a session? I want it to turn something on, or start, or something when they log in, and go away when they log out, so I can make log-in only pages.


http://i46.tinypic.com/dw7zft.png

Offline

 

#2 2011-01-26 07:57:20

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Need PHP Coders...

Hmm, maybe using a database that sends info to say they are online? Or maybe a boolean(true/false)?

Offline

 

#3 2011-01-26 08:09:48

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

ProgrammingFreak wrote:

Hmm, maybe using a database that sends info to say they are online? Or maybe a boolean(true/false)?

No, using SESSIONS.


http://i46.tinypic.com/dw7zft.png

Offline

 

#4 2011-01-26 10:27:13

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Need PHP Coders...

Oh that's easy. I did something like this when I created a DNS Server (My own version of the internet. Domain Names have no limits in extensions  smile  ).

Let me find the login code.


The following code was made by me. If you use this or base your code of this please link back to me. Post this copyright information on your page somewhere.

Login system based on code developed and copyrighted by <a href="http://http://scratch.mit.edu/users/what-the">what-the</a>. Contact what-the for more information on the login system and it's use.

This is the login system. Edit the text printed to fit site.

Code:

<?php
session_start();
//Database Information

$dbhost = "localhost";
$dbname = "database";
$dbuser = "XXXX";
$dbpass = "XXXX";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());


Print"<head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\"><title>Confirming login</title><link rel=\"icon\" type=\"image/ico\" href=\"/*/database/favicon.ico\"></head>";
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM `users` WHERE `username` LIKE '$username' and `password` LIKE'$password'";
$result = mysql_query($query) or die ( "An error has occured.");

if (mysql_num_rows($result) != 1) {
$error = "Bad Login";
    include "login.html";
    Print"Username and password combo is incorrect";
} else {

    $query = "SELECT * FROM `users` WHERE `username` LIKE '$username' and `password`     LIKE'$password' and `active` =1";
    $result = mysql_query($query) or die ( "An error has occured.");
    if (mysql_num_rows($result) != 1) {
        $error = "Username not activated";
        Print"You have not activated your account. Please check your email address that you provided for instructions on how to active your account.";
            include "login.html";

    }else{
            $_SESSION['username'] = "$username";
            include "memberspage.php";
        
    }
}
mysql_close();

?>

This is the code to check that the user is loged in

Code:

<?
session_start();
Print"<head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\"><title>Main</title><link rel=\"icon\" type=\"image/ico\" href=\"/*/database/favicon.ico\"></head>";

if ( empty( $username ) ) {

    print "Please login below!";

    include 'login.html';

} else {


Print"Welcome $username";
//Include Loged in html here using print or echo
}
?>

http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#5 2011-01-26 19:44:03

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

Doesn't work. When I log in and go to the secure page, it redirects me back to log in. (Keep in mind, this is my log in system integrated with yours...) I don't think

Code:

if ( empty( $username ) ) {

    print "Please login below!";

    include 'login.html';

} else {

this part works. $username was never defined.

Anyway, here's my code:

(DON'T STEAL ANY PART OF IT!!! YES THAT MEANS YOU PROGRAMMINGFREAK!)

Code:

<?
session_name("MyLogin");
session_start();

if($_GET['action'] == "login") {
$conn = mysql_connect("mysql13.000webhost.com","???????","?????");
$db = mysql_select_db("a7044783_1");
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM users WHERE login='$name'");

if(mysql_num_rows($q_user) == 1) {

$query = mysql_query("SELECT * FROM users WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION['username'] = "$username";

header("Location: yourpage.php"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}

// if the session is not registered

if ( empty( $username ) ) {
header("Location: login.php");

and

Code:

<?

session_start();
if ( empty( $username ) ) {
header("Location: login.php");

}
?>
Log in successful!

Last edited by SeptimusHeap (2011-01-26 19:46:03)


http://i46.tinypic.com/dw7zft.png

Offline

 

#6 2011-01-26 20:58:40

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Need PHP Coders...

SeptimusHeap wrote:

Doesn't work. When I log in and go to the secure page, it redirects me back to log in. (Keep in mind, this is my log in system integrated with yours...) I don't think

Code:

if ( empty( $username ) ) {

    print "Please login below!";

    include 'login.html';

} else {

this part works. $username was never defined.

Anyway, here's my code:

(DON'T STEAL ANY PART OF IT!!! YES THAT MEANS YOU PROGRAMMINGFREAK!)

Code:

<?
session_name("MyLogin");
session_start();

if($_GET['action'] == "login") {
$conn = mysql_connect("mysql13.000webhost.com","???????","?????");
$db = mysql_select_db("a7044783_1");
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM users WHERE login='$name'");

if(mysql_num_rows($q_user) == 1) {

$query = mysql_query("SELECT * FROM users WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION['username'] = "$username";

header("Location: yourpage.php"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}

// if the session is not registered

if ( empty( $username ) ) {
header("Location: login.php");

and

Code:

<?

session_start();
if ( empty( $username ) ) {
header("Location: login.php");

}
?>
Log in successful!

Lol your problem is that you never defined username you called it name.
$_SESSION['username'] = "$username"

Should be
$_SESSION['username'] = "$name"


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#7 2011-01-26 21:10:21

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Need PHP Coders...

I don't see $username defined anywhere. Is this supposed to be the username value passed in by POST?


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#8 2011-01-26 21:50:49

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Need PHP Coders...

If you want to make checking for errors easy indent you open brackets like I do.

If Random {
     Something
     Moresomething
} else {
    If MoreRandom {
         Somethingsomemore
    }
}

Also after every 100 lines. Write a note saying what line it is. This makes finding errors easy as well. Trust me counting over 300 lines in non-indented code trying to find one error is annoying.

fullmoon wrote:

I don't see $username defined anywhere. Is this supposed to be the username value passed in by POST?

That's what I poined out in my above post.

Last edited by what-the (2011-01-26 21:51:56)


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#9 2011-01-26 23:06:18

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Need PHP Coders...

what-the wrote:

Also after every 100 lines. Write a note saying what line it is. This makes finding errors easy as well. Trust me counting over 300 lines in non-indented code trying to find one error is annoying.

Or just use a code editor with line numbers  tongue


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#10 2011-01-27 07:15:10

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

:'(

The user thing helped, but the yourpage.php (the second one) is redirecting me to the log in. Either that, or the log in page is.


http://i46.tinypic.com/dw7zft.png

Offline

 

#11 2011-01-27 07:40:22

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Need PHP Coders...

You'll figure it out. Just look at my code compaired to your current code. Check that you are getting a cookie when you login.


Ok here
Look at your log in code. You do not finish the if statment.


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#12 2011-01-27 07:44:39

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

what-the wrote:

You'll figure it out. Just look at my code compaired to your current code. Check that you are getting a cookie when you login.


Ok here
Look at your log in code. You do not finish the if statment.

What one? I counted the {s and }s... Oh, the last one? I fixed that already.

And I'm going to add cookies later, for now, I want a session.

Last edited by SeptimusHeap (2011-01-27 07:46:31)


http://i46.tinypic.com/dw7zft.png

Offline

 

#13 2011-01-27 07:52:23

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Need PHP Coders...

SeptimusHeap wrote:

what-the wrote:

You'll figure it out. Just look at my code compaired to your current code. Check that you are getting a cookie when you login.


Ok here
Look at your log in code. You do not finish the if statment.

What one? I counted the {s and }s... Oh, the last one? I fixed that already.

And I'm going to add cookies later, for now, I want a session.

Session is cookie. That's why I said check your cookies to see if you get one when you log in.


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#14 2011-01-27 08:08:58

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

Kay.


http://i46.tinypic.com/dw7zft.png

Offline

 

#15 2011-01-27 08:12:19

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

OK, the cookie MyLogin is there.


http://i46.tinypic.com/dw7zft.png

Offline

 

#16 2011-01-28 17:06:25

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

Bump.


http://i46.tinypic.com/dw7zft.png

Offline

 

#17 2011-01-28 19:42:24

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

Bump...

Why is nobody helping?


http://i46.tinypic.com/dw7zft.png

Offline

 

#18 2011-01-29 08:00:24

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

Okay, really? Bump.


http://i46.tinypic.com/dw7zft.png

Offline

 

#19 2011-01-29 08:22:47

recycle49
Scratcher
Registered: 2009-12-21
Posts: 1000+

Re: Need PHP Coders...

cant help dude


"Every challenge must be met, every battle must be won, and every story will end." -Me
Recycle49 December 09 - November 11 Goodbye

Offline

 

#20 2011-01-29 10:31:45

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

sad


http://i46.tinypic.com/dw7zft.png

Offline

 

#21 2011-01-31 07:14:44

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

Bump :'(


http://i46.tinypic.com/dw7zft.png

Offline

 

#22 2011-01-31 14:35:52

comp500
Scratcher
Registered: 2010-01-08
Posts: 1000+

Re: Need PHP Coders...

what-the wrote:

SeptimusHeap wrote:

what-the wrote:

You'll figure it out. Just look at my code compaired to your current code. Check that you are getting a cookie when you login.


Ok here
Look at your log in code. You do not finish the if statment.

What one? I counted the {s and }s... Oh, the last one? I fixed that already.

And I'm going to add cookies later, for now, I want a session.

Session is cookie. That's why I said check your cookies to see if you get one when you log in.

Only sometimes. Local storage and session storage in javascript are not cookies.


800 posts! W00T! Oh sorry im not on a lot but at least i have 1000+ posts

Offline

 

#23 2011-01-31 15:52:39

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Need PHP Coders...

http://i302.photobucket.com/albums/nn100/fullmoon32/Screenshot2011-01-31at24914PM.png
You can find your PHP cookies here in the Safari or Chrome dev menus (right click anything and choose Inspect Element -- you have to enable this in Safari's Preferences).


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#24 2011-01-31 16:45:54

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Need PHP Coders...

Yes, okay, the cookie is there. Can someone help now?


http://i46.tinypic.com/dw7zft.png

Offline

 

#25 2011-02-01 02:07:40

comp500
Scratcher
Registered: 2010-01-08
Posts: 1000+

Re: Need PHP Coders...

fullmoon wrote:

http://i302.photobucket.com/albums/nn10 … 4914PM.png
You can find your PHP cookies here in the Safari or Chrome dev menus (right click anything and choose Inspect Element -- you have to enable this in Safari's Preferences).

Same with localstorage and sessionstorage. VERY useful.


800 posts! W00T! Oh sorry im not on a lot but at least i have 1000+ posts

Offline

 

Board footer