Jump to content

Start a session name from a result from a login


Russia

Recommended Posts

hey guys, Im trying to register a session from a login im making and for some reason its not working.

 

 

here is my code:

 

<?php    
session_start();
if(isset($_POST['username'])){
$username = $_POST['username']; //name of the text field for usernames
$password = $_POST['password']; //likewise here just for the password
//connect to the db
$user = 'root'; 
$pswd = ''; 
$db = 'chat';
$conn = mysql_connect('localhost', $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = "SELECT * FROM users WHERE username = '$username' AND password ='$password'";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

//this is where the actual verification happens
    if(mysql_num_rows($result) == 1){
    //the username and password match
    //so e set the session to true
$_SESSION['username'] = $username;
$_SESSION['uID'] = $result['user_id'];
	//$_SESSION['email'] = $result['email'];


    //and then move them to the index page or the page to which they need to go
    header('Location: index.php');
    }else{
    $err = 'Incorrect username / password.' ;
    }
    //then just above your login form or where ever you want the error to be displayed you just put in
    echo $err;
}
else

?>

 

Im trying to make it so it also gets the user_id of the user logging in and creates a session for it.

 

It works for the username part, and Im able to echo the username im logged in with, but for some reason it does want to work for the user_id part.

 

This is what doesnt register

	$_SESSION['uID'] = $result['user_id'];

 

Thanks for the upcoming help.

Link to comment
Share on other sites

Before you can perform database operations on your data in your application, you must learn how to execute a query and retrieve the data from that query. Here's a basic SELECT query example (if you only expect one row, you don't need to use a loop when fetching the data from the result resource) - http://w3schools.com/php/php_mysql_select.asp

Link to comment
Share on other sites

$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

 

after this line add the following,

$row = mysql_fetch_array($result);

 

this will fetch one row and save it in the $row variable as array, now suppose you want to access name field in that row,

so you need to simply point to the $row[name]

 

I hope you would have got what i mean to say.

 

Link to comment
Share on other sites

Before this line,

 

$_SESSION['uID'] = $result['user_id'];

 

You need to give the $result array some actual values.  Right now that variable is just a resource identifier for your mysql_query.  You can use any of the mysql_fetch_array(), mysql_fetch_assoc(), or mysql_fetch_object() functions to do this.  I'm more of an OOP guy so I prefer mysql_fetch_object, but for your purposes, any of them would work equally well.

 

$assoc = mysql_fetch_assoc($result);
$_SESSION['uID'] = $assoc['user_id'];

 

Hope this helps.

 

More about these functions: mysql_fetch_array() | mysql_fetch_assoc() | mysql_fetch_object()

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.