Jump to content

How do I...


Pi_Mastuh

Recommended Posts

I want to make my site save a session so the user doesn't need to login everytime they com to the site. If they've logged in previously I want them to still be login when they go to my site and it will re-direct to a certain page if they're logged in. Anyone know how to do that?
Link to comment
Share on other sites

when they log-in send a cookie with like [b]autologin=1[/b] if they want to stay logged in forever.

Then  you just need something to request that cookie and if autologin is 1 then set their session. you'll obviosly wanna put their userid and any info you wanna load into sessions in the cookie as well
Link to comment
Share on other sites

You could set a cookie that lasts for a year at a time, so unless the user cleans up his cookies (deletes them) it will be an autologin feature for a year at a time.

Example to set a cookie after login success:
[code]

<?php

if($login_ok) // simplified example
{
// set a cookie that will work for 365 days (one year)
setcookie("user", "Donald Duck", time()+60*60*24*365,"/",".Duckburg_yourdomain.com",0);
}

?>

[/code]

And on your main page, check if this cookie already exists - something like this:
[code]

<?php

if(isset($_COOKIE['user']) && !empty($_COOKIE['user']))
{
  // your cookie is found
  $user = htmlspecialchars($_COOKIE['user'], ENT_QUOTES);
 
  print "Hello {$user}, Welcome back!";
 
  // or redirect
  header("Location: members.php");
  exit();
}
else
{
  // no cookie found matching your check
  print "Hello guest, If you already have an account, please log in first";
}

?>

[/code]

For understanding cookies, use the manual: http://no2.php.net/manual/en/function.setcookie.php
Link to comment
Share on other sites

I've got the cookie creating part:

[code]$expire = time()+60*60*24*365; // Expire in 1 year
 
  setcookie("preuserID", "$preuserID", $expire);
  setcookie("preuserPassword", "$password", $expire);

header("Location: ../home.php");
exit;[/code]

and the fetching:

[code]<?
if(isset($_COOKIE['preuserID']) && !empty($_COOKIE[';preuserID']) and
isset($_COOKIE['preuserPassword']) && !empty($_COOKIE[';preuserPassword']))
{
  // your cookie is found
  $preuserID = htmlspecialchars($_COOKIE['preuserID'], ENT_QUOTES);
  $preuserPassword = htmlspecialchars($_COOKIE['preuserPassword'], ENT_QUOTES);

  header(Location: /reg/home.php);
  exit();
}
?>[/code]

which is imbeded in my index.htm, but it's not redirecting. Anyone know why?
Link to comment
Share on other sites

Delete your html file and only have a .php file.

Is this your server or hosted? If it's yours you can change the settings, if you're on a shared host, you can't, and you'll have to have just the .php file.
If you search the web for more info, you'll find it.
Link to comment
Share on other sites

default of any webserver that allows php is to check for index.html, index.htm, and then index.php among others..just copy your code from your .html file, create a file as index.php not index.php.html mind you..and clear any other index.wtfever else there is in that dir and you should be good to go..
Link to comment
Share on other sites

1&1 Linux packages allow PHP, if you have a 1&1 windows package, then ASP is what you can run.

I also use 1&1, using the linux package which is a full on LAMP config. If you have the Windows package, you should be able to swap it to the linux package, thus giving you php & Mysql Access.

Nate
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.