Advertisement
gitlez

YA: Check User Cookie For Login WoC 20130620074829AAJvlEe

Jun 20th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. <?php
  2. // Yahoo Ansers' Question: http://answers.yahoo.com/question/answer?qid=20130620074829AAJvlEe
  3.  
  4. // Variable to hold the message we want displayed.
  5. $message = '';
  6.  
  7.  
  8. // Check and see if the $_COOKIE['username'] exists
  9. If(isset($_COOKIE['username'])) {
  10.    
  11.     require_once('connect.php');
  12.    
  13.     $username = mysql_real_escape_string( $_COOKIE['username'] );
  14.    
  15.     $sql = "SELECT user_id FROM users WHERE username='{$username}' LIMIT 1";
  16.    
  17.     $result = mysql_query($sql);
  18.    
  19.     // Check to make sure statement and query are good.
  20.     if( !$result ){
  21.         $message .= 'Internal Error: ' . mysql_error();
  22.     }else if( mysql_num_rows($result) === 0){
  23.         $message .= 'Welcome, Guest!<br />';
  24.     }else{
  25.         $row = mysql_fetch_assoc($result);
  26.         $user_id = $row['user_id'];
  27.  
  28.         $message .= 'Welcome . ' . $username . '!<br /><br />';
  29.         $message .= 'Your <a href="profile.php?id=' . $user_id . '">Profile Page</a> awaits.';
  30.     }
  31. } else {
  32.     $message .= "Welcome, Guest! <br/>";
  33. }
  34.  
  35. ?>
  36. <html>
  37.     <head>
  38.         <title>Home</title>
  39.         <style type="text/css">
  40.             a:link {
  41.                 color: blue;
  42.                 text-decoration: none;
  43.             }
  44.             a:visited {
  45.                 color: purple;
  46.                 text-decoration: none;}
  47.             a:hover {
  48.                 color: black;
  49.                 text-decoration: none;
  50.             }
  51.             h3 {
  52.                 text-align: center;
  53.             }
  54.         </style>
  55.     </head>
  56.     <body>
  57.         <h3>Home</h3>
  58.         <hr />
  59.         <br />
  60.         <br />
  61.         <?php echo $message; ?>
  62.     </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement