Jump to content
xisto Community
Sign in to follow this  
apple

Using Sessions Instead Of Cookies, Help Please

Recommended Posts

This is a simple code to register and login.. this uses cookies.. i want to use sessions instead.. can someone tell how i can do it ?
config.php

<?   ob_start(); // allows you to use cookies   $conn = mysql_connect("localhost","USER","PASSWORD");   mysql_select_db(DATEBASE) or die(mysql_error());   //fill in the above lines where there are capital letters.   $logged = MYSQL_QUERY("SELECT * from users WHERE id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");   $logged = mysql_fetch_array($logged);   //the above lines get the user's information from the database.?>



login.php
<?oB_start();// allows you to use cookies.include("config.php");if (!$logged[username]){if (!$_POST[login]){echo("<center><form method=\"POST\"><table><tr><td align=\"right\">Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\"></td></tr><tr><td align=\"right\">Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\"></td></tr><tr><td align=\"center\"><input type=\"submit\" name=\"login\" value=\"Login\"></td></tr><tr><td align=\"center\"><a href=\"register.php\">Register Here</a></td></tr></table></form></center>"); }if ($_POST[login]) {// the form has been submitted.  We continue...$username=$_POST['username'];$password = md5($_POST[password]);// the above lines set variables with the submitted information.   $info = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());$data = mysql_fetch_array($info);if($data[password] != $password) {// the password was not the user's password!echo "Incorrect username or password!";}else{// the password was right!$query = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());$user = mysql_fetch_array($query);// gets the user's informationsetcookie("id", $user[id],time()+(60*60*24*5), "/", "");setcookie("pass", $user[password],time()+(60*60*24*5), "/", "");// the above lines set 2 cookies. 1 with the user's id and another with his/her password.   echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://forums.xisto.com/no_longer_exists/ You! You will be redirected");// modify the above line...add in your site url instead of yoursite.com}}}else{// we now display the user controls.echo ("<center>Welcome <b>$logged[username]</b><br /></center>- <a href=\"editprofile.php\">Edit Profile</a><br />- <a href=\"members.php\">Member List</a><br />- <a href=\"logout.php\">Logout</a><br />");}?>

Share this post


Link to post
Share on other sites

This is a simple code to register and login.. this uses cookies.. i want to use sessions instead.. can someone tell how i can do it ?



Your're confusing the terms a bit here. A session really isn't an alternative to using cookies. When using a session, the default behavior is to store the session using a cookie, which will happen automatically. Check out the documentation of start_session(), which you need to call to use sessions:

http://dk2.php.net/manual/en/function.session-start.php

If you don't want to rely on cookies at all, you either add the Session ID (SID) to all your urls/links or enable automatic url rewriting. You can read more about this here:

http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.