Pi_Mastuh Posted January 6, 2007 Share Posted January 6, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/ Share on other sites More sharing options...
psychohagis Posted January 6, 2007 Share Posted January 6, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154258 Share on other sites More sharing options...
Pi_Mastuh Posted January 6, 2007 Author Share Posted January 6, 2007 How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154261 Share on other sites More sharing options...
alpine Posted January 6, 2007 Share Posted January 6, 2007 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]<?phpif($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]<?phpif(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 Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154266 Share on other sites More sharing options...
Pi_Mastuh Posted January 6, 2007 Author Share Posted January 6, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154404 Share on other sites More sharing options...
Pi_Mastuh Posted January 6, 2007 Author Share Posted January 6, 2007 ??? Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154534 Share on other sites More sharing options...
alpine Posted January 6, 2007 Share Posted January 6, 2007 ...missing quotes inside your header call, and it cannot run inside a htm file ! Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154536 Share on other sites More sharing options...
magic2goodil Posted January 6, 2007 Share Posted January 6, 2007 agree on the missing quotes..also depending on how your calling your page, if your get any errors with headers already sent, u may try echoing some javascript to redirect the page..that is worst case though, but alas have had to do it before. Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154555 Share on other sites More sharing options...
Pi_Mastuh Posted January 7, 2007 Author Share Posted January 7, 2007 But if Idon't run it on the htm file how do I run it on the homepage? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154557 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 lol, i believe he was joking saying you must run php code in a file with .php file extension. Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154559 Share on other sites More sharing options...
Pi_Mastuh Posted January 7, 2007 Author Share Posted January 7, 2007 No... PHP can be run in an HTML file as long as it's enclosed by <? and ?> Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154568 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 Yes, but the file extension must be .php unless you have configured your server to parse .html as php. Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154577 Share on other sites More sharing options...
Pi_Mastuh Posted January 7, 2007 Author Share Posted January 7, 2007 Is there a way I can make the index.html file a .php? So when they first go to my site it's a php file? Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154582 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154586 Share on other sites More sharing options...
Pi_Mastuh Posted January 7, 2007 Author Share Posted January 7, 2007 I have hosting with 1&1 Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154593 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154595 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 So apply my previous statement... Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154597 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 indeed..sorry i meant to quote u and forgot jesirose Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154601 Share on other sites More sharing options...
Pi_Mastuh Posted January 7, 2007 Author Share Posted January 7, 2007 I made the page a php file and added the quotes and it's still not working. Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154624 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 [b]What[/b]'s not working?Are you sure the php is running? Add something like echo 'test'; in it to be sure. Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154629 Share on other sites More sharing options...
Pi_Mastuh Posted January 7, 2007 Author Share Posted January 7, 2007 what would I echo though? Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154633 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 The word test would have been echoed hence the 'test' you might want to create a separate file called test.phpput in it:<?php phpinfo(); ?>and then run that file to test that your server is even allowing php...who is your hosting service? Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154637 Share on other sites More sharing options...
Pi_Mastuh Posted January 7, 2007 Author Share Posted January 7, 2007 1and1, I knwo it allows php because my whole site is PHP Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154653 Share on other sites More sharing options...
chronister Posted January 7, 2007 Share Posted January 7, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154657 Share on other sites More sharing options...
Jessica Posted January 7, 2007 Share Posted January 7, 2007 [quote author=jesirose link=topic=121266.msg498572#msg498572 date=1168131366][b]What[/b]'s not working?Are you sure the php is running? Add something like echo 'test'; in it to be sure.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/33109-how-do-i/#findComment-154660 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.