473,549 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem setting cookies to recognize logged in user.

19 New Member
I am trying to set cookies in two different scenarios:
  1. When users sign up.
  2. When users login.

and then they will be directed to main.php where main.php includes header.php. Now all the problem is with header.php when users login they are redirected to main.php where header menu should be home, profile, logout etc but when users who are not logged in visit main.php. header menus should be sign up and login.
register.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysqli_connect("localhost","root","","findfriends") or die ("Connection not established");
  3. ?>
  4. <?php include ( "./inc/header.inc.php" ); ?>
  5. <?php
  6.  
  7.  if(isset($_POST['reg'])){
  8. $fn=strip_tags(@$_POST['fname']);
  9. $ln=strip_tags(@$_POST['lname']);
  10. $un=strip_tags(@$_POST['username']);
  11. $em=strip_tags(@$_POST['email']);
  12. $pswd=strip_tags(@$_POST['password']);
  13. $d= date("Y-m-d");
  14.  
  15. $reg_query = "INSERT INTO users (userid,username,first_name,last_name,email,password,sign_up_date,activated,bio,profile_photo,closed) VALUES ('','{$un}','{$fn}','{$ln}','{$em}','{$pswd}','{$d}','0','What you do?','','no')";
  16. $reg_run = mysqli_query($con,$reg_query); 
  17.      setcookie('user',$uname,time()+3600*24*365);  
  18.       echo "The cookie has been set."; 
  19.  
  20. header("Location:main.php");
  21.  
  22. }
  23. ?>
  24.  
  25. <?php
  26. if(isset($_POST['login'])){ 
  27. $log_que = "SELECT * FROM users WHERE username = '$un' AND password = '$pswd' ";
  28. $log_run = mysqli_query($con, $log_que);
  29. $row = mysql_fetch_array($log_run);
  30. $un_db = $log_row['username'];
  31. $pswd_db = $log_row['password'];
  32.  
  33. if($un == $un_db && $pswd == $pswd_db)
  34. {
  35.     echo "LOGGED IN!";  
  36.      setcookie('user',$uname,time()+3600*24*365); 
  37.       header("Location:main.php");
  38. }
  39. }
  40. ?>
  41.  
  42.   <div style="width: 800px; margin: 0px auto 0px auto;">
  43.   <table>
  44.     <tr>
  45.       <td width="60%" valign="top">
  46.        <h2>Already a member? Login Below</h2>
  47.         <form action="" method="POST">
  48.           <input type="text" name="user_login" size="25" placeholder="UserName"/><br><br>
  49.           <input type="password" name="password_login" size="30" placeholder="Passsword"/><br><br>
  50.           <input type="submit" name="login" value="Login">
  51.         </form>
  52.       </td>
  53.       <td width="40%" valign="top">
  54.         <h2>Sign Up!</h2>
  55.         <form action="" method="POST">
  56.           <input type="text" name="fname" size="25" placeholder="First Name"/><br><br>
  57.           <input type="text" name="lname" size="25" placeholder="Last Name"/><br><br>
  58.           <input type="text" name="username" size="25" placeholder="Username"/><br><br>
  59.           <input type="email" name="email" size="25" placeholder="Email"/><br><br>
  60.           <input type="password" name="password" size="25" placeholder="Password"/><br><br>
  61.           <input type="submit" name="reg" value="Sign Up!">
  62.         </form>
  63.       </td>
  64.     </tr>
  65.   </table>
header.php
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3.  <head>
  4.   <title>findfriends</title>
  5.   <script src="js/main.js" type="text/javascript"></script>
  6.   <link rel="stylesheet" type="text/css" href="./css/style.css"/>
  7.  
  8.  </head>
  9.  <body>
  10.  
  11.   <div class="headerMenu">
  12.     <div id="wrapper">
  13.       <div class="logo">
  14.         <img src="./img/logo.gif"/>
  15.       </div>
  16.       <div class="search_box">
  17.         <form action="searchresults.php" method="post" name="search">
  18.           <table>
  19.            <tr>
  20.             <td>  
  21.     <input type="text" name="search" placeholder="Search ..."/>
  22.             </td>
  23.             <td>
  24.     <input type="image" src="./img/search-icon.png" alt="submit" />
  25.             </td>
  26.           </tr>
  27.           </table>
  28.        </form>
  29.       </div>
  30.  
  31.       <div class="cb">
  32.          <a href="create_blog.php">Create a Blog</a>
  33.       </div>
  34.  
  35.  
  36.       <?php
  37.       if(isset($_COOKIE['user'])){
  38.         echo '
  39.  
  40.           <ul class="dd">
  41.  
  42.            <li><a href="main.php" >Home</a></li>
  43.            <li><a href="' . $user . '">Profile</a></li>
  44.            <li><a href="my_messages.php">Inbox' . $unread_numrows . '</a></li> 
  45.          <li><a href="#">Management</a>
  46.          <ul><li><a href="account_settings.php">Settings</a></li>
  47.           <li><a href="logout.php">Logout</a></li>
  48.           </ul>
  49.           </li>
  50.           </ul>';
  51.  
  52.       }
  53.       else
  54.       {
  55.         echo '
  56.         <ul class="dd">
  57.         <li><a href="register.php" >Sign Up</a></li>
  58.  
  59.         <li><a href="register.php">Login</a></li>
  60.         </ul>';
  61.       }
  62.     ?>
  63.  
  64.  
  65.     </div>
  66.   </div>
  67.  </body>
  68. </html>
main.php is empty with just with hello world to test header working.
Sep 22 '15 #1
0 1012

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

Similar topics

2
1380
by: Ian Sedwell | last post by:
Hi guys I've come across a problem with cookies. It seems that if one attempts to save several cookies in quick succession and then read them back again, you may attempt to read a cookie that has not yet been saved from the first operation. I assume that this is because JavaScript via the browser hands the job over to the OS and then...
6
2324
by: zoltix | last post by:
Hi, I would like to access to execute specific commands but the privileges are not enough from an aspx Page. Because this service run as IU_IISSERVER, therefore aspx hasn’t access to these resources and it is normal.. But sometimes, I need these privileges for special commands. How to execute some “lines code” for other user than currently...
8
2254
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing happens. dim filt as string ... build filter string... UserControl.ReportFilter = filt
2
1758
by: Greg | last post by:
Please note: I have cross posted this from Newsgroup: microsoft.public.dotnet.framework.aspnet.webservices with a few minor changes... I am having a simple problem setting up the security on my test web service... My Web service code is: Imports System.Web.Services
12
2645
by: Patxi | last post by:
This is the first time I try to use cookies, and despite of reading some tutotials, I'm have real trouble to make it work correctly. My cookie reading code is in Master Page. When I click on a test button (in a WEb User Control inside the aspx page), cookie value is displayed in Master Page but if I go to another page, no value is...
5
36971
by: mvr | last post by:
Hi all How to get the Windows current logged user name using Classic ASP. If no direct way are there any work arounds. Thanks mvr
1
2577
by: Greg Hurlman | last post by:
I have an HttpModule that captures the FormsAuthenticationModule.Authenticate event, and replaces the HttpContext principal with a custom principal. The code is straightforward enough, yet when an ASPX page tries to get this custom principal it fails - the object I inserted is not there. I've tried having this module declaration at both the...
4
6444
by: Vlad Dogaru | last post by:
Hello, I am trying to use cookies and Python to create a simple login example. But I am very disoriented at the existence of two cookie libraries, namely Cookie and cookielib. I have seen examples of setting cookies (although I am still not sure about timestamps and cookie lifespan), but no reference to getting the currently set cookies....
0
973
by: Shwan | last post by:
Dear developers, I working on C# .NET, I have a problem how to prevent a user to login in to the system while another one already logged in using the Same User Name And Password. I tried to put a Flag in the Users Table but to reset the Flag I have to be sure that the user should Sing out and not close the system immediately, So please if you...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.