Welcome Guest ( Log In | Register )




                Web Hosting Guide

Writing And Testing My Own Login Script [solved]
Eggie
post Feb 21 2008, 06:24 PM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 212
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735
myCENTs:10.93


i have this error
QUOTE
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/eggie/public_html/race.php:2) in /home/eggie/public_html/race.php on line 5

in every page i have with session start...what's the problem??


CODE
<title>Race</title>
<?php
include("style.css");
include("config.php");
session_start();
if(!session_is_registered(myusername)){
echo 'Your Session has Expired!';
exit;}

//If you click race...
if ($_GET['action']=='race')
{
$asa=$_POST['bike'];

if(!isset($asa))
{
echo 'You didn\'t select any bike!';
?>
<br>click <a href=race.php> HERE</a> to go back
<?php
exit;
}

else{

$rand=mt_rand(1,2);
  
if ($_POST['bike']==1) $trub='Derbi Senda 50';
if ($_POST['bike']==2) $trub='Honda NS 50 R';
if ($_POST['bike']==3) $trub='Suzuki ZR 50';
if ($_POST['bike']==4) $trub='Yamaha DT 50 MX';
if ($_POST['bike']==5) $trub='Aprilia RS 50';

echo'You raced against:';
echo $trub;
$name=$_SESSION['views'];


$res = mysql_query("select * from members where username = '$name'");
$wins1 = mysql_fetch_array($res);
$points=$wins1['points'];
$wo=$wins1['wins'];
$lo=$wins1['lose'];

//If random number is equal to 1 than set win +1
if($rand==1){
?>
</br>
<?php
$wo++;
$points=$points+100;
mysql_query("update members set wins=$wo where username = '$name'");
mysql_query("update members set points=$points where username = '$name'");
echo '<br>You won 100 points!';
?>
</br>
<table border="1">
<?php
echo'You have total of: ' .$points. ' points!';
?>
</table>
</br>
<?php
}

//else lose +1
else {

echo'<br><br>You lost!';
?>
<table border="1"><td>
<?php

echo'You have total of: ' .$points. ' points!';


?>
</td></table>

<?php
$lo++;

mysql_query("update members set lose=$lo where username = '$name'");

?>
</br>
<?php
}

//How many times you won overall
echo 'Wins:';
echo $wo;
?>
</br>
<?php
echo'Losses:';
echo $lo;


//race form
?>
<form name="form3" method="post" action="race.php">

    <input type=submit value="Go back">  

</form>
<?php



exit;


}
}

if ($_GET['action']!='race'){?>
<html>
<head>
&lt;script type="text/javascript">
    function showpic(o) {
        o = parseInt(o);
        switch (o)
        {
        case 1:
            imgName='pictures/senda.jpg';break;
        case 2:
            imgName='pictures/ns.jpg';break;
        case 3:
            imgName='pictures/zr.jpg';break;
        case 4:
            imgName='pictures/dt.jpg';break;
        case 5:
            imgName='pictures/rs.jpg';break;
        }
        var DivImage = document.getElementById('DivImg');
        var DisplayImg = document.getElementById('IdImg');
        DisplayImg.src=imgName;
        DivImage.style.visibility="visible";
    }
</script>
</head>
<body>
<form name="form2" method="post" action="race.php?action=race">
<table border="1"><td>Who do you wanna race?<br>
<font size="1">Hint:You get 100 points for racing this class</font></td></table>
    <select size=5 name="bike" onchange="showpic(this.options[this.selectedIndex].value)">
    <option value="1">Derbi Senda 50</option>
    <option value="2">Honda NS 50 R</option>
    <option value="3">Suzuki ZR 50</option>
    <option value="4">Yamaha DT 50 R</option>
    <option value="5">Aprilia RS 50</option>
    </select><br>
    <input type=submit value=Race>  
    <div id="DivImg" style="position:relative;top:-90px;left:150px;z-index:20;width:25px;height:25px;visibility:hidden;text-align:center;"><img id="IdImg"></div>
</td>
</tr>
</form>

</body>
</html>

<?php
}
?>
<form method=post action='login_success.php'>
                         <input type=submit value='Go to main!'></form>
Go to the top of the page
 
+Quote Post
3 Pages V  < 1 2 3 >  
New Topic
Replies (10 - 19)
Eggie
post Feb 21 2008, 09:23 PM
Post #11


Premium Member
Group Icon

Group: [HOSTED]
Posts: 212
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735
myCENTs:10.93


i tried the code but it didnt work...now i would like your code so i can try it
Go to the top of the page
 
+Quote Post
toby
post Feb 21 2008, 09:24 PM
Post #12


Super Member
Group Icon

Group: Members
Posts: 611
Joined: 29-September 06
Member No.: 16,228


Move html and printing php to the end, and check everything is a valid function.
Go to the top of the page
 
+Quote Post
Feelay
post Feb 21 2008, 09:32 PM
Post #13


Kinda N00B
Group Icon

Group: Members
Posts: 235
Joined: 13-January 08
From: Sweden
Member No.: 27,579


CODE
<?php
include("style.css");
include("config.php");
// username and password sent from signup form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['login'];
header("location:login_success.php");
$_SESSION['views'] = $_POST['myusername'];
}
else {
echo "Wrong Username or Password";
}
?>


Try this. The part I changed was this:

CODE
session_register(myusername);
session_register(mypassword);


I erased them, and put $_SESSION['login'] Instead.
But now, you will have to change every place where you have:
CODE
session_register(myusername);
session_register(mypassword);

into
CODE
$_SESSION['login'];

even in the login succes and so on. and in your logout.php you will have to change it to:

CODE
<?php
session_start();
session_unset();
session_destroy();
?>


This is the way I do it. It should work.

Edit:

Between.. You should change this in your login_succes.php.

change from:

CODE
if(!session_is_registered(myusername)){
header("location:main_login.php");
}


to:

CODE
if(!isset($_SESSION['login'])){
header("location:main_login.php");
}


If think thats what you wanted or? if the user is not logged in, he will be redirected to main_login.php. if you want the difference, just remove the "!" before "isset". And you will have to write this "if(!isset($_SESSION['login'])){..." instead of "if(!session_is_registered(myusername)){..." in every file that you want to protect.

This post has been edited by Feelay: Feb 21 2008, 09:37 PM
Go to the top of the page
 
+Quote Post
Eggie
post Feb 21 2008, 09:36 PM
Post #14


Premium Member
Group Icon

Group: [HOSTED]
Posts: 212
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735
myCENTs:10.93


btw...how do you check if the session is registered??
that if the session is not registered that it just print out "Session is not registered"
Go to the top of the page
 
+Quote Post
Feelay
post Feb 21 2008, 09:41 PM
Post #15


Kinda N00B
Group Icon

Group: Members
Posts: 235
Joined: 13-January 08
From: Sweden
Member No.: 27,579


very simple wink.gif

CODE
<?php
if(isset($_SESSION['login'])){
echo "Welcome;) You are logged in";
}else{
echo "Welcome Guest! You are not logged in";
}
?>


And you can ofc do it the other way too wink.gif

CODE
<?php
if(!isset($_SESSION['login'])){
echo "Welcome Guest! You are not logged in";
}else{
echo "Welcome;) You are logged in!";
}
?>


in the last one, if you look carefullt, you will see that I inserted a "!" before i wrote "isset". "!" = not wich means:

QUOTE
If not isset session login, echo welcome guest. you are not loged in, else echo welcome, you are logged in.
in the first one, it would be:

QUOTE
if isset session login, echo welcome. you are logged in, else echo welcome guest. you are not logged in.


This post has been edited by Feelay: Feb 21 2008, 09:42 PM
Go to the top of the page
 
+Quote Post
Eggie
post Feb 21 2008, 09:51 PM
Post #16


Premium Member
Group Icon

Group: [HOSTED]
Posts: 212
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735
myCENTs:10.93


i dont know why but there is nothing in my $_SESSION['login']
CODE
<?php
include("style.css");
include("config.php");
// username and password sent from signup form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['login'];
$_SESSION['views'] = $_POST['myusername'];
header("location:login_success.php");

}
else {
echo "Wrong Username or Password";
}
?>

since this is the code...something's wrong with rows

This post has been edited by Eggie: Feb 21 2008, 09:54 PM
Go to the top of the page
 
+Quote Post
mastercomputers
post Feb 22 2008, 01:49 AM
Post #17


Making IT Happen
Group Icon

Group: Members
Posts: 653
Joined: 1-September 04
From: Auckland, New Zealand
Member No.: 27
myCENTs:57.31


1:
You have <title>Race</title> before your PHP session/headers, this means the headers will already be sent and you were too late to start a session. Usually caching a page before it's sent can solve this, or just move the title below the PHP code.

2:
Including a CSS stylesheet, I don't know what's inside here, but if it's plain text use HTML to include the stylesheet or if it's PHP, give it a PHP extension, else it'll be treated just like plain text.

e.g. <link href="path/to/style.css" rel="stylesheet" type="text/css" />

3:
session_start() must appear before any other PHP operation unless page caching is done, but you should learn to always do it first.

4:
session_is_registered() takes a string, you are using a constant called myusername which may or may not be what you want, e.g. you might want to do if(!session_is_registered('myusername'))

5:
failed to check if $_GET['action'] is set before using it, you should do:

if(!empty($_GET['action']) && $_GET['action'] == 'race')

6:
failed to check if $_POST['bike'] exists, undefined variables can cause undesirable results, always insure they are set before using them.

if(!empty($_POST['bike'])) {
$asa = $_POST['bike'];
}
else {
echo '<p>You did not select a bike!</p><p>Click <a href="race.php">HERE</a> to go back</p>';
exit;
}

I know there will be more problems, but I don't have time to go through the whole thing at the moment so I'll leave it as this for now.


Cheers,

MC
Go to the top of the page
 
+Quote Post
yordan
post Feb 22 2008, 02:49 PM
Post #18


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 2,976
Joined: 16-August 05
Member No.: 7,896
myCENTs:44.60


By the way, I changed the topic title, the old one ("Login") was not clear enough. The new title should be more representative of the problem statement and the solutions found. rolleyes.gif
Go to the top of the page
 
+Quote Post
Feelay
post Feb 23 2008, 05:06 PM
Post #19


Kinda N00B
Group Icon

Group: Members
Posts: 235
Joined: 13-January 08
From: Sweden
Member No.: 27,579


Sorry. My fault. Try this code:

CODE
<?php
include("style.css");
include("config.php");
// username and password sent from signup form
$myusername=$_POST['myusername'];
$myusername=mysql_real_escape_string($myusername);//I added this to make your script a little more safe
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['login'] =$myusername; //I added "=$myusername, so that the session can recognize you"
$_SESSION['views'] = $_POST['myusername'];
header("location:login_success.php");

}
else {
echo "Wrong Username or Password";
}
?>


Try it. It Should Work.

//Feelay

This post has been edited by Feelay: Feb 23 2008, 05:07 PM
Go to the top of the page
 
+Quote Post
Eggie
post Feb 23 2008, 05:12 PM
Post #20


Premium Member
Group Icon

Group: [HOSTED]
Posts: 212
Joined: 19-January 08
From: Zagreb/Croatia
Member No.: 27,735
myCENTs:10.93


hey man...sorry i already solved it and it works fine...smile.gif
sorry for not posting so you can mark it [SOLVED]
if you wanna see the game i'm making go to MY GAME
i will put it on this server when i finish it smile.gif
thanx for the help feelay
Go to the top of the page
 
+Quote Post

3 Pages V  < 1 2 3 >
Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new 80 OpaQue 11,398 19th November 2009 - 02:21 PM
Last post by: starscream
No New Posts 5 Niru 3,806 5th November 2009 - 08:04 AM
Last post by: iG-balajee
No New Posts  
4 PHP
7 bluefish 2,804 3rd November 2009 - 09:29 PM
Last post by: HannahI
No New Posts   4 yordan 232 29th October 2009 - 11:29 AM
Last post by: iG-Oobles
No New Posts   10 yordan 2,226 27th October 2009 - 11:31 PM
Last post by: HannahI
No New Posts   2 Ronel 1,710 24th October 2009 - 08:27 PM
Last post by: iG-firoz
No New Posts   2 pbolduc 2,768 3rd October 2009 - 08:20 PM
Last post by: iG-Andrew
No new   22 TavoxPeru 10,420 2nd October 2009 - 07:53 AM
Last post by: iG-Mel
No New Posts   3 Eggie 1,274 2nd October 2009 - 12:45 AM
Last post by: iG-candy
No New Posts   11 soleimanian 4,210 22nd September 2009 - 12:01 PM
Last post by: iG-
No New Posts   7 Eggie 2,079 9th September 2009 - 02:22 AM
Last post by: iG-nate
No New Posts 9 doudou 3,585 6th September 2009 - 08:30 PM
Last post by: iGuest
No New Posts   12 m3th 6,637 29th August 2009 - 12:16 AM
Last post by: dmnhunter
No New Posts   3 techocian 1,273 28th August 2009 - 03:18 AM
Last post by: iG-joombee
No New Posts   1 FirefoxRocks 2,094 25th August 2009 - 07:21 AM
Last post by: iG-sharma


Web Hosting Powered by ComputingHost.com.