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
https://forums.phpfreaks.com/topic/183663-loggin-with-session-help/
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.

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

here is a cool tutorial to learn about cookies

http://php.about.com/od/finishedphp1/ss/php_login_code.htm

 

and to set session on your page on top start

session_start();

then in your page give your variable names like this

$user = $_SESSION['username'];
$pass = $_SESSION['pass'];

etc then you can check if that session exists or not on other pages.

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.

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
} 

Archived

This topic is now archived and is closed to further replies.

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