Jump to content

Loggin with session help??


renlfey

Recommended Posts

Ok so here is my delema...    i would to create a session with user name call 'userlog' so that if the client is logged in it will display a welcome back 'BOB' instead of the login form

 

here is the login(i know its probable wrong but hey im new at this!.

 

                          <?php

session_start();

if(isset($_SESSION['$userlog']))

echo 'Weclome back', '$username';

else

include("includes/login-mod.php");

 

?>

 

and here is the checklogin script (the one were i need to create a session!

 

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="rigid gaming"; // Database name

$tbl_name="users"; // Table name

 

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// username and password sent from form

$username=$_POST['username'];

$password=$_POST['password'];

 

// To protect MySQL injection (more detail about MySQL injection)

$username = stripslashes($username);

$password = stripslashes($password);

$username = mysql_real_escape_string($username);

$password = mysql_real_escape_string($password);

 

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";

$result=mysql_query($sql);

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched $username and $password, table row must be 1 row

 

if($count==1){

// Register $username, $password and redirect to file "success.php"

session_register("username");

session_register("password");

header("location:../success.php?success==0");

}

else {

header("location:../success.php?success==1");

}

?>

 

Any help would be greatly apreciated !!!!!!

Link to comment
Share on other sites

I'm still learning PHP, but I'm pretty sure sessions are destroyed when the browser is closed. You may to look into using cookies. Cookies will be stored in the users browser until the user deletes them(most users with no knowledge of PC's deletes them, lol) Do a quick search on storing login cookies with PHP and see what you get.

Link to comment
Share on other sites

True but i dont want the users to automaticly log in the next time they visit the site! , so sessions i think are the way for me to go since the user must login in order to view content its just cookie get saved and session i think your right on this are destroyed wich is what im looking for!

 

But i guess if somone know how to edit my code for cookies also that would be cool too lol

Link to comment
Share on other sites

just by having  a cookie established, doesn't mean you have to have the user automatically logged backed in. The browser would just recoginize that this user has been here before and then from there, you could do whatever you want. Such as saying Welcome back for the 25th time, or 26th time, etc.

Link to comment
Share on other sites

if you wants to create a cookie that is also as you create session nearly here is the syntex:

$hour = time() + 3600;
setcookie(User, $_POST['username'], $hour);
setcookie(pass, $_POST['pass'], $hour); 

so first you give name as User and then give value and on last is expire tiem 3600seconds from the time cookie starts. so after 3600 second means one hour cookie will expire and user need to login again. and on pages you need to check if cookie is set you need something like this :

 

if(isset($_COOKIE['User']))
{ 
//your statements
}else {
//back to login page nothing to display
} 

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.