Jump to content

[SOLVED] Session/login


bhavin_85

Recommended Posts

hey guys

 

ive just written this code for a simple log in form but im having some trouble with the session variable

 

<?
session_start();

$username=$_POST['username'];	
$password=$_POST['password'];
echo $username;
echo $password;

include('config.php');

$sql="SELECT * FROM regcustomer WHERE username='$username' AND password='$password'"; 
$query=mysql_query($sql) or die("Queryfailed:".mysql_error());
if (mysql_num_rows($query) != 0) { 
$row = mysql_fetch_assoc($query);
$_SESSION['username'] = $row['username'];
$username=$_SESSION['username'];
echo "$username";
}
//header("Location:default.php");

?>

 

when i echo the session variable it doesnt print anything, however if in the sql statement i put a username and password it works....i dont know where im going wrong!

 

please help! cheers

 

 

Link to comment
Share on other sites

hey guys thanks 4 the replies but thats not the actual problem

 

the problem is with the if statement

 

$sql="SELECT * FROM regcustomer WHERE username='$username' AND password='$password'"; 
$query=mysql_query($sql) or die("Queryfailed:".mysql_error());
if (mysql_num_rows($query) != 0) { 
$row = mysql_fetch_assoc($query);
$_SESSION['username'] = $row['username'];
echo "hell";
}
else {
echo "shit dont work";
} 

 

it doesnt seem to run the if it just jumps to the else

 

however if i put actual values from the table in $username and $password it runs the if.

 

theres nothing wrong with the form that brings the values as i have echoed them and they are correct

 

any1 know whats wrong? ???

Link to comment
Share on other sites

Try this simple test:

 

<?php

  $sql = "SELECT * FROM regcustomer WHERE username='$username' && password='$password'"; 
  $query = mysql_query($sql);

  $count = mysql_num_rows($query);

  if($count < 1) {

  echo'This Sucks Nuts';
  }

    else {
    echo'Hell jes';
    }

?>

   

 

Link to comment
Share on other sites

<?php
session_start();

$username=$_POST['username'];	
$password=$_POST['password'];
echo $username;
echo $password;

include('config.php');

$sql="SELECT * FROM regcustomer WHERE username='$username' && password='$password'"; 
$query=mysql_query($sql) or die("Queryfailed:".mysql_error());
$count=mysql_num_rows($query);
if($count != 0){
//if (mysql_num_rows($query) != 0) { 
//$row = mysql_fetch_assoc($query);
//$_SESSION['username'] = $row['username'];
echo "hell";
}
else {
echo "shit dont work";
} 
?>

 

right at the top...its coming from the form on the page before...i know that its passing the right value aswell cuz ive echoed them and they are correct :)

Link to comment
Share on other sites

Hi

 

I would do it like this i havent tested it but it should work

 

<?php
session_start();

$username = addslashes($_POST['username']);	
$password = addslashes($_POST['password']);

include('config.php');

$sql = "SELECT * FROM regcustomer WHERE username='$username' AND password = '$password'"; 
$query = mysql_query($sql) or die (mysql_error());
$row = @mysql_fetch_array($query);
if ($row)
  {
echo "It worked <br />";
$_SESSION['username'] = $row['username'];
$user = $_SESSION['username'];
echo "Logged in as $user <br />";
}else{
echo "Wrong username or password";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.