473,466 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Persistent Cookie Help Required!

39 New Member
This is a login.php.

The user is given a choice to either input his/her Username & Password or Email & Password. In short, either log-in inputting your Username or your Email.

It is written in mysqli procedural. I have not learned pdo oop yet. I need help in the login.php to add the "Remember Me" feature using Cookies. I have googled but most tutorials teach to save the user password in the cookie! And that is a big NO! NO!
Therefore, I do not trust these tutorials any more. But, I trust the php folks here!
Can someone be the Great Samaritan here to show me an example code of how the cookie part should be coded in php ? You're welcome to not start from scratch but work on my work (login.php).
registration.php, logout.php and account_acivation.php finished. Those last 3 files are working fine. Working on the home.php now.


login.php

Expand|Select|Wrap|Line Numbers
  1.     <?php
  2.  
  3.     /*
  4.     ERROR HANDLING
  5.     */
  6.     declare(strict_types=1);
  7.     ini_set('display_errors', '1');
  8.     ini_set('display_startup_errors', '1');
  9.     error_reporting(E_ALL);
  10.     mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  11.  
  12.     include 'config.php';
  13.  
  14.     // check if user is already logged in
  15.     if (is_logged() === true) 
  16.     {
  17.         //Redirect user to homepage page after 5 seconds.
  18.         header("refresh:2;url=home.php");
  19.         exit; //
  20.     }
  21.  
  22.  
  23.     if (isset($_POST["login_username_or_email"]) && 
  24.     isset($_POST["login_password"]))
  25.         {
  26.             $username_or_email = trim($_POST["login_username_or_email"]);
  27.             $password = $_POST["login_password"];        
  28.  
  29.             //Select Username or Email to check against Mysql DB if they are 
  30.             already registered or not.
  31.  
  32.             if(strpos("$username_or_email", "@"))
  33.             {
  34.                 $email = $username_or_email;
  35.  
  36.                 $query = "SELECT ids, usernames, passwords, emails, 
  37.                 accounts_activations_statuses FROM users WHERE emails = ?";
  38.                 $stmt = mysqli_stmt_init($conn);
  39.                 $stmt = mysqli_prepare($conn, $query);            
  40.                 mysqli_stmt_bind_param($stmt, 's', $email);
  41.                 mysqli_stmt_execute($stmt);
  42.                 //$result = mysqli_stmt_get_result($stmt); //Which line to use ? 
  43.                 This line or the next ?
  44.                 $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, 
  45.                 $db_password, $db_email, $db_account_activation_status); // 
  46.                 Which line to use ? This line or the one above ?
  47.             }
  48.             else
  49.             {
  50.                 $username = $username_or_email;
  51.  
  52.                 $query = "SELECT ids, usernames, passwords, emails, 
  53.                 accounts_activations_statuses FROM users WHERE usernames = ?";
  54.                 $stmt = mysqli_stmt_init($conn);
  55.                 $stmt = mysqli_prepare($conn, $query);
  56.                 mysqli_stmt_bind_param($stmt, 's', $username);
  57.                 mysqli_stmt_execute($stmt);
  58.                 $result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, 
  59.                 $db_password, $db_email, $db_account_activation_status); // 
  60.                 Which line to use ? This line or the one above ?
  61.             }
  62.  
  63.             $row = mysqli_stmt_fetch($stmt);        
  64.             mysqli_stmt_close($stmt);
  65.  
  66.             if (!password_verify($password, $db_password))
  67.             {
  68.                 echo "Incorrect User Credentials!';<br>";
  69.                 exit();
  70.             }
  71.             else
  72.             {
  73.                 $_SESSION["user"] = $db_username;            
  74.                 header("location:home.php?user=$db_username");    
  75.             }
  76.         }    
  77.     ?>
  78.  
Oct 12 '17 #1
0 1685

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: obhayes | last post by:
Hi All, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the...
1
by: brad | last post by:
Hi, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the...
1
by: Marco Rispoli | last post by:
I am using this function to write a cookie: private static void SaveCookie(string CookieName, string CookieValue, bool Persistent) { HttpContext.Current.Response.Cookies.Value = CookieValue;...
0
by: Kepler | last post by:
I'm testing very basic FormsAuthentication and having trouble with non-persistent cookies. Once authenticated with a non-persistent cookie, if I leave the browser alone for 30 minutes,...
4
by: craigkenisston | last post by:
I have an asp.net application in which I sometimes store a persistent cookie once the user has logged in and this has been working great. However, I now add some user information like, username,...
8
by: bashful.belle | last post by:
I'm using Forms authentication and a non persistent cookie in my asp.net application. How do i get the cookie to time out after a period of inactivity, say 10 minutes, and force the user to login...
0
by: robert | last post by:
I have noticed in .net 2 that when authenticating a user, setting the cookie using either redirectfromloginpage or setauthcookie, specifying true for the persistent parameter that the cookie is...
4
by: GaryDean | last post by:
I'm using the 2.0 login control with the "remember me" setting. When checked the cookie only last for a few hours then it is asking again for a login. I don't see any time settings. I know back...
8
by: WiW | last post by:
FYI: It appears that Phorm (a targeted advertising system which taps into ISP networks) will be setting its own persistent cookie for most every website the user visits. It appears as though the...
3
by: rh.krish | last post by:
I have a typical ASP.NET 2.0 Forms authentication application which authenticates against Active Directory. I use non-persistent cookie so that the user is NOT remembered across browser sessions....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.