Jump to content
MakeWebGames

Can someone help me?


Recommended Posts

Posted

Ok so im really bad with sessions and i need help. Heres the code.

Problem: Sessions arent passing user data correctly

index.php

<?php

session_start();
if(get_magic_quotes_gpc() == 0)
{
foreach($_POST as $k => $v)
{
 $_POST[$k]=addslashes($v);
}
foreach($_GET as $k => $v)
{
 $_GET[$k]=addslashes($v);
}
}

switch($_GET['do']){
case 'auth': auth(); break;
default: index(); break;
}


function index(){
print '<table width="100%" cellspacing="1" cellpadding="1" style="text-align:center;"><tr><th>Login to Hardcore Mafia</th></tr>
<form action="index.php?do=auth" method="post">
<tr><td>
Username:<input type="text" name="username"></td></tr>
<tr><td>
Password:<input type="password" name="password"></td></tr>
<tr><td>
<input type="submit" value="Login!"></td></tr>
</form>
</table>';
}

function auth(){
$host="*Blanked out*";
$user="*Blanked out*";
$pass="*Blanked out*";
$database="*blanked out*";

$cid = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($database,$cid) or die(mysql_error());

if($_POST['username']=="" || $_POST['password']==""){
die("Please Fill in All Fields");
}

$userdata = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}' AND password='{$_POST['password']}'") or die(mysql_error());

if(mysql_num_rows($uq)==0){
die("Invalid Login!");
}

else{
$_SESSION['loggedin']=1;
$fetch = mysql_fetch_row($uq);
header("Location: loggedin.php");
}
}


?>

 

loggedin.php

<?php
session_start();
$host="dkdkdt";
$user="ejkmf";
$pass="ejfkdf";
$database="ejkdksdkjsdi983838";

$cid = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($database,$cid) or die(mysql_error());

$userid=$_SESSION['userid'];

$uquery = mysql_query("SELECT * FROM users WHERE userid={$userid}",$cid);
$udata = mysql_fetch_row($uquery);

print "Hey! {$udata['username']}";

?>

 

When i login, i t doesnt display the username data, and in phpmyadmin, loggedin still equals 0.

Posted

oops

well its not exactly mccodes but if i post in in the php forum it never gets answered. and i changed $ir to $udata like i have it

Posted

You didn't define $_SESSION['userid'] in the authenticate (just scanned over it quickly)

so maybe afew edits could be editing line 48

$userdata

into

$uq

and below the fetch_row (line 56) maybe add

$_SESSION['userid'] = $fetch['userid'];

 

NOTE: this is index.php

Posted

Well, ive tried that too crimgame, it just doesnt seem to want to pass the sessions through correctly. i fixed all the definitions and added that thing you asked me too, and it still wont work :/

Posted
<?php

session_start();
if(get_magic_quotes_gpc() == 0)
{
foreach($_POST as $k => $v)
{
 $_POST[$k]=addslashes($v);
}
foreach($_GET as $k => $v)
{
 $_GET[$k]=addslashes($v);
}
}

switch($_GET['do']){
case 'auth': auth(); break;
default: index(); break;
}


function index(){
print '<table width="100%" cellspacing="1" cellpadding="1" style="text-align:center;"><tr><th>Login to Hardcore Mafia</th></tr>
<form action="index.php?do=auth" method="post">
<tr><td>
Username:<input type="text" name="username"></td></tr>
<tr><td>
Password:<input type="password" name="password"></td></tr>
<tr><td>
<input type="submit" value="Login!"></td></tr>
</form>
</table>';
}

function auth(){
$host="*Blanked out*";
$user="*Blanked out*";
$pass="*Blanked out*";
$database="*blanked out*";

$cid = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($database,$cid) or die(mysql_error());

if($_POST['username']=="" || $_POST['password']==""){
die("Please Fill in All Fields");
}

$userdata = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}' AND password='{$_POST['password']}'") or die(mysql_error());

if(mysql_num_rows($userdata)==0){
die("Invalid Login!");
}

else{
$_SESSION['loggedin']=1;
$fetch = mysql_fetch_array($userdata);
$_SESSION['userid'] = $fetch['userid'];
header("Location: loggedin.php");
}
}


?>

Try that.

Posted

hey

ok it was working, for some reason when i logout its not letting me login again. and it wont let 2 users on at the same time

Posted

Okay, well i made a simple check(php file), to check the sessions.

<?php

session_start();
if($_SESSION['loggedin']) { $check1 = "Yeap"; } else { $check1="Nope"; }
if($_SESSION['mobid']) { $check2 = $_SESSION['mobid']; } else { $check2 = "Invalid!"; $_SESSION['mobid']=1; }

print "Logged In: {$check1}
Mob Id:{$check2}";

?>

 

Basically that will tell me which ones are working. When i go there without doing login.php first, it says nope to everything, which is good.

But when i do login and go there, it returns Logged In: Yeap Mob ID: Invalid.

I have it so it sets my mob id to 1 if it isnt set, but thats just so i can get it under ID 1.

So basically my logged in is being set but my mob id isnt

Posted

Try this:

<?php

session_start();
if(get_magic_quotes_gpc() == 0)
{
foreach($_POST as $k => $v)
{
 $_POST[$k]=addslashes($v);
}
foreach($_GET as $k => $v)
{
 $_GET[$k]=addslashes($v);
}
}

switch($_GET['do']){
case 'auth': auth(); break;
default: index(); break;
}


function index(){
print '<table width="100%" cellspacing="1" cellpadding="1" style="text-align:center;"><tr><th>Login to Hardcore Mafia</th></tr>
<form action="index.php?do=auth" method="post">
<tr><td>
Username:<input type="text" name="username"></td></tr>
<tr><td>
Password:<input type="password" name="password"></td></tr>
<tr><td>
<input type="submit" value="Login!"></td></tr>
</form>
</table>';
}

function auth(){
$host="*Blanked out*";
$user="*Blanked out*";
$pass="*Blanked out*";
$database="*blanked out*";

$cid = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($database,$cid) or die(mysql_error());

if($_POST['username']=="" || $_POST['password']==""){
die("Please Fill in All Fields");
}

$userdata = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}' AND password='{$_POST['password']}'") or die(mysql_error());

if(mysql_num_rows($userdata)==0){
die("Invalid Login!");
}

else{
$_SESSION['loggedin']=1;
$fetch = mysql_fetch_array($userdata);
$_SESSION['mobid'] = $fetch['userid'];
header("Location: loggedin.php");
}
}


?>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...