Jump to content

Recommended Posts

I'm am somewhat new to PHP and am trying to set up a website for my cousin's wedding. Her idea is to have the guests sign in with a user/pass that she provides, and once they sign in, they will be taken to a page that has their name on it (i.e. "Mr. and Mrs. So and So, you are invited...). I have come to the conclusion that I will need to make an image for each guest's name (she wants to use a font for their names that nobody will have on their computer) so what I need to know is:

 

How do I link each user name to their own personalized webpage, where the image of their name on the next page will change based on what username is entered? The php code I have right now is this (i'm sorry it's so long, I just don't want to leave anything out that might be important):

 

$LOGIN_INFORMATION = array(
  'steve' => 'password',
  'rick' => 'password',
  'tom'=> 'password'
);

// request login? true - show login and password boxes, false - password box only
define('USE_USERNAME', true);

// User will be redirected to this page after logout
define('LOGOUT_URL', 'http://www.example.com/');

// time out after NN minutes of inactivity. Set to 0 to not timeout
define('TIMEOUT_MINUTES', 0);

// This parameter is only useful when TIMEOUT_MINUTES is not zero
// true - timeout time from last activity, false - timeout time from login
define('TIMEOUT_CHECK_ACTIVITY', true);

##################################################################
#  SETTINGS END
##################################################################


///////////////////////////////////////////////////////
// do not change code below
///////////////////////////////////////////////////////

// show usage example
if(isset($_GET['help'])) {
  die('Include following code into every page you would like to protect, at the very beginning (first line):<br><?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?>');
}

// timeout in seconds
$timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);

// logout?
if(isset($_GET['logout'])) {
  setcookie("verify", '', $timeout, '/'); // clear password;
  header('Location: ' . LOGOUT_URL);
  exit();
}

if(!function_exists('showLoginPasswordProtect')) {

// show login form
function showLoginPasswordProtect($error_msg) {
?>
<html>
<head>
  <title>Please enter password to access this page</title>
  <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
  <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body,td,th {
font-family: Verdana, Geneva, sans-serif;
font-size: 10px;
color: #666;
}
body {
background-color: #FFFFFB;
}
</style>
</head>
<body>
<div align="center">
  <style>
    input { border: 1px solid black; }
  </style>
  <div style="width:600px; margin-left:auto; margin-right:auto; text-align:center">
  <form method="post">
    <h4>Please sign in using the information provided on the invitation</h4>
    <font color="red"><?php echo $error_msg; ?></font><br />
<?php if (USE_USERNAME) echo 'Login:<br /><input type="input" name="access_login" /><br />Password:<br />'; ?>
    <input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" />
  </form>
  <br />
  <a style="font-size:9px; color: #B0B0B0; font-family: Verdana, Arial;" href="http://www.zubrag.com/scripts/password-protect.php" title="Download Password Protector">Powered by Password Protect</a>
  </div>
</body>
</html>

<?php
  // stop at this point
  die();
}
}

// user provided password
if (isset($_POST['access_password'])) {

  $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
  $pass = $_POST['access_password'];
  if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)
  || (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) ) 
  ) {
    showLoginPasswordProtect("Incorrect password.");
  }
  else {
    // set cookie if password was validated
    setcookie("verify", md5($login.'%'.$pass), $timeout, '/');
    
    // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
    // So need to clear password protector variables
    unset($_POST['access_login']);
    unset($_POST['access_password']);
    unset($_POST['Submit']);
  }

}

else {

  // check if password cookie is set
  if (!isset($_COOKIE['verify'])) {
    showLoginPasswordProtect("");
  }

  // check if cookie is good
  $found = false;
  foreach($LOGIN_INFORMATION as $key=>$val) {
    $lp = (USE_USERNAME ? $key : '') .'%'.$val;
    if ($_COOKIE['verify'] == md5($lp)) {
      $found = true;
      // prolong timeout
      if (TIMEOUT_CHECK_ACTIVITY) {
        setcookie("verify", md5($lp), $timeout, '/');
      }
      break;
    }
  }
  if (!$found) {
    showLoginPasswordProtect("");
  }

}

?>

 

**************************************************************

 

THANK YOU SO MUCH for any help!!

Sam

That's a lot to go through so I'll just give you the methodology.  I see you're using a cookie, but if you used sessions it would store a cookie and allow you to persist variables across pages. So start a session and once they have loggedin, store their username in a session var.  Then you can retrieve this on any page and use it to determine the images etc.

 

$_SESSION['username'] = $_POST['access_login'];

 

// then on other pages

 

<img src="images/<?php echo $_SESSION['username']; ?>.png">

 

 

Shawn,

 

I think I'm close to understanding what you're telling me, but how do I use sessions instead of cookies? Is it safe to take all the cookie code out of that and replace it with the code you gave me or is there a lot more to it than that?

 

(I'm probably a lot newer to PHP than I should be, but there's a first for everything!) I really do appreciate your help!!

 

Sam

Wouldn't if work with your situation even if you still used cookies,

 

if ($username == "tim") {
echo "<img src=img/tim.jpg>";
} elseif ($username == "bob") {
echo "<img src=img/bob.jpg>";
} elseif ($username == "grace") {
echo "<img src=img/bob.jpg>";
}
?>

 

Supercode!

 

EDIT: Totally wasn't thinking when I wrote that... made a few silly mistakes.

The Eagle,

 

I will try that out tonight, thank you!!

 

Yep, fixed it a bit. Also, if the list is huge, like I'd assume a wedding list would be, having 30 elseif statments isn't exactly poetry. If that's your case, you can go to AbraCadaver's solution with PHP cookies.

 

A short example, if there is a password to gain access, you may need a MySQL database.

 

Here's how you can use a cookie,

 

$_POST['username'];
$username = $username;

setcookie("Cookie", $username);
setcookie("Cookie", $username, time()+3600);  // one hour expiration

 

EDIT: Wow.. I can't read today. So he said use SESSIONS, sorry.

 

<?php
session_start();

$_SESSION['username'] = $username;
$_SESSION['time'] = time();

?>

 

Then you can look toward his solution,

 

<img src="images/<?php echo $_SESSION['username']; ?>.png">

I would go in a totally different way, and let PHP generate the images, all you have to do is pass the name to the PHP image file.  The steps are.

 

1. Choose your font file.  You can use any True Type Font, or .ttf file, some located Here

 

2. Make you a PHP file named image.php, it should look like:

image.php

<?php

// Set the content-type
header('Content-type: image/png');

$text = (isset($_GET['text'])) ? preg_replace('~[^a-zA-Z\.\s]~','',$_GET['text']) : exit();

$font = 'A.C.M.E._Explosive_Bold.ttf';  //location of your true type font. File name should NOT have any spaces.

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 150, 150, 150);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// Add some shadow to the text
imagettftext($im, 20, 0, 31, 30, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 30, 29, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

 

3. After login, set their name to a $_SESSION variable.

//Login is complete.
$_SESSION['user'] = $userId;

 

4. Pass the user id to the image file through an html image tag.

<img src="image.php?text=<?php echo $_SESSION['user']; ?>" alt="none" />
Or for testing
<img src="image.php?text=John%20Smith" alt="none" />

 

 

I'm am somewhat new to PHP and am trying to set up a website for my cousin's wedding.

 

When became wedding's so complicated?

 

Since women decided it was fun ;)

I hope you do know there are ways to include a font in a website, instead of making a million images?

Just google for  @font css, might not be extremly legal, but it's just another way of doing it.

 

- edit: I didnt saw jc bones post ;)

Wouldn't if work with your situation even if you still used cookies,

 

if ($username == "tim") {
echo "<img src=img/tim.jpg>";
} elseif ($username == "bob") {
echo "<img src=img/bob.jpg>";
} elseif ($username == "grace") {
echo "<img src=img/bob.jpg>";
}
?>

 

 

The Eagle,

 

I think this is my best solution with the little knowledge that I have in PHP. I understand the code you gave me, but I can't get it to work and it's driving me insane! I pasted that into my password php document, right under the Login array info (and I'm not getting any syntax errors, which makes me think it's okay?) ... then in the file for the page that I want the login to take them to, I pasted this:

 

<?php echo $username; ?>, you are invited...

 

 

But when I test it out, nothing shows up except the ", you are invited..." 

 

I also tried the embedded fonts method:

 

if ($username == "tim") {

echo "Mr. and Mrs. Tim", you are invited...

}

 

and ",you are invited" shows up in my font, but it's blank before that, just like when I try to make an image show up.

Are you able to tell me what I'm doing wrong??

 

Thank you!!

 

 

That's a lot to go through so I'll just give you the methodology.  I see you're using a cookie, but if you used sessions it would store a cookie and allow you to persist variables across pages. So start a session and once they have loggedin, store their username in a session var.  Then you can retrieve this on any page and use it to determine the images etc.

 

$_SESSION['username'] = $_POST['access_login'];

 

// then on other pages

 

 

<img src="images/<?php echo $_SESSION['username']; ?>.png">

 

 

 

I dont know if you are done with this or not but definitely go with this. It will be very easy and work perfectly for what you want.

That's a lot to go through so I'll just give you the methodology.  I see you're using a cookie, but if you used sessions it would store a cookie and allow you to persist variables across pages. So start a session and once they have loggedin, store their username in a session var.  Then you can retrieve this on any page and use it to determine the images etc.

 

$_SESSION['username'] = $_POST['access_login'];

 

// then on other pages

 

 

<img src="images/<?php echo $_SESSION['username']; ?>.png">

 

 

 

I dont know if you are done with this or not but definitely go with this. It will be very easy and work perfectly for what you want.

I really really really appreciate every body's help and apologize for being so dense on this subject! I'd love to go with the easiest route... I just have no idea where to put this SESSION code into what I already have. What do I replace it with, or is it additional to what I already have?

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.