renlfey Posted December 2, 2009 Share Posted December 2, 2009 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 !!!!!! Quote Link to comment Share on other sites More sharing options...
justAnoob Posted December 2, 2009 Share Posted December 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
renlfey Posted December 2, 2009 Author Share Posted December 2, 2009 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 Quote Link to comment Share on other sites More sharing options...
mraza Posted December 2, 2009 Share Posted December 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
renlfey Posted December 2, 2009 Author Share Posted December 2, 2009 OK Awsome, as a noob please bare with me!!!! I have successfully created and displayed the name that i choose but now another issue how do i go about creating the cookie with the username?? Quote Link to comment Share on other sites More sharing options...
justAnoob Posted December 2, 2009 Share Posted December 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
mraza Posted December 2, 2009 Share Posted December 2, 2009 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 } Quote Link to comment Share on other sites More sharing options...
renlfey Posted December 2, 2009 Author Share Posted December 2, 2009 ok so it does create the cookie but the second part does not work <?php if(isset($_COOKIE['username'])) { echo "Welcome " . $_COOKIE["username"] . "!<br />"; } else include("../include/login-form.php"); ?> BTW THX Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.