FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need help with Members area/link adder





DanielXP
I have a members area that works on sessions/cookies and i also have a link adder with:

* Your Username
* Your Email
* Link Name
* Link URL

I want to change the code of the link adder remove the textbox of the Your Username and in the add.php i would like it to get the session from the members area to post there user name

This is the login page:

Code:
<?
ob_start();
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
if (isset($_POST["phpAuth_Submit"])) {

      include "phpAuth_config.php";
      $filename = $database_name;
      
   if (!file_exists($filename)) {
         
         echo "No database exists. Please check your config file.";
         
   } else {
      
      $handler = fopen($filename,'r');
      $members = file_get_contents($filename);
      // get username and password
      $users_array = explode ("\n",$members);
      
      foreach ($users_array as $users) {
         if ($users != "" || $users != NULL) {
      
            // break username and password string into seperate variables.
            list($username,$password) = explode("\t",$users);
               
            if ($_POST["phpAuth_usr"] == trim($username) && md5($_POST["phpAuth_pwd"]) == trim($password)) {   

               $logged_in = 1;
               break;
               
            } else {
                  
               $logged_in = 0;
                  
            }
               
         } else {
         
            break;
         
         }
         
      } // end foreach
      
      if ($logged_in != 1) { // IF USER IS NOT LOGGED IN
         
         echo "Error! Incorrect Details. <a href=\"".$_SERVER['HTTP_REFERER']."\">Go Back</a>";
      
      } else { // ELSE LOGGED IN         
               
         session_start();
         $_SESSION["phpAuth_username"] = $_POST["phpAuth_usr"];
         $_SESSION["phpAuth_password"]  = $_POST["phpAuth_pwd"];
         
            if ($_POST["phpAuth_rm"] == 1) {
               setcookie("phpAuth_username",$_POST["phpAuth_usr"],time()+3600);
            }
         
         header('Location: '.$login_redirect.'');
      }   
   
   } // END IF FILE EXISTS
   
} else {   
      
?>
<form name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
  <table width="100%"  border="0" cellpadding="2" cellspacing="0">
    <tr>
      <td><strong>Username</strong></td>
    </tr>
    <tr>
      <td><input name="phpAuth_usr" type="text" id="phpAuth_usr" value="<? if (isset($_COOKIE["phpAuth_username"])) { echo $_COOKIE["phpAuth_username"]; } ?>"></td>
    </tr>
    <tr>
      <td><strong>Password</strong></td>
    </tr>
    <tr>
      <td><input name="phpAuth_pwd" type="password" id="phpAuth_pwd"></td>
    </tr>
    <tr>
      <td>Remember Me
        <input name="phpAuth_rm" type="checkbox" id="phpAuth_rm" value="1"></td>
    </tr>
    <tr>
      <td>Not Registered? <a href="members/user_adduser.php" target='blank'>Register Here</a> </td>
    </tr>
    <tr>
      <td><input name="phpAuth_Submit" type="submit" id="phpAuth_Submit" value="Login"></td>
    </tr>
  </table>
</form>
<?
}
?>
</body>
</html>
<?
ob_end_flush();
?>



as you can see it starts the session
Code:
         session_start();
         $_SESSION["phpAuth_username"] = $_POST["phpAuth_usr"];
         $_SESSION["phpAuth_password"]  = $_POST["phpAuth_pwd"];



Below is the link adder code:


Code:
<?php

include "style.php";
$filename = 'link.txt'; // edit this

function addlink($link_name,$link_url,$by,$email) {
global $filename;
$html = "<a href='$link_url' target='blank'>$link_name</a>, posted by <a href='mailto:$email'>$by\n";
$fp = fopen($filename, 'a+');


function logIP($link_name)
{
     $ipLog="log.txt"; // Your logfiles name here (.txt or .html extensions ok)


     $register_globals = (bool) ini_get('register_gobals');
     if ($register_globals) $ip = getenv(REMOTE_ADDR);
     else $ip = $_SERVER['REMOTE_ADDR'];

     $date=date ("l dS of F Y h:i:s A");
     $log=fopen("$ipLog", "a+");

     if (preg_match("/\bhtm\b/i", $ipLog) || preg_match("/\bhtml\b/i", $ipLog))
     {
          fputs($log, "IP: $ip - Link added: $link_name <br>");
     }
     else fputs($log, "IP: $ip - Link added: $link_name \n");

     fclose($log);
}
// Place the below function call wherever you want the script to fire.
logIp($link_name);


fputs($fp, $html) or die("Could not open file!");
echo 'The link was added successfully.<br><br>';

}

function showlinks() {
global $filename;
$html = implode('<br/>',array_reverse(file($filename)));
echo $html;
}

If (!$link_url){
   print "You must enter a Link Address";
} elseif (!$link_name){
   print "You must enter a Link Name";
} elseif (!$by){
   print "You must enter your Name";
} elseif (!$email){
   print "You must enter a Email Address";
} else {
   addlink($link_name,$link_url,$by,$email);
   print "<b>Existing Links</b>:<br>";    // adds new link
   showlinks();                      // display's list of links
}


?>



If any one can help, That will be cool.
DanielXP
Can any one help?
Rhysige
Im only skimming through so cant look at your code.. but are you trying to just replace a text input with the name from the session?
DanielXP
Yes i want to delete the input 'Your Username' and edit the add.php and make it get the username from the session.
mathiaus
Just change this function
Code:
function showlinks() {
global $filename;
$html = implode('<br/>',array_reverse(file($filename)));
echo $html;
}

// Set session username to $by
$by = $_SESSION["phpAuth_username"];

If (!$link_url){
   print "You must enter a Link Address";
} elseif (!$link_name){
   print "You must enter a Link Name";
} elseif (!$email){
   print "You must enter a Email Address";
} else {
   addlink($link_name,$link_url,$by,$email);
   print "<b>Existing Links</b>:<br>";    // adds new link
   showlinks();                      // display's list of links
}


You didn't post the actual form so there just remove the <input name=usernamename> part
DanielXP
Parse error: parse error, unexpected $ in links/add.php on line 93
DanielXP
i moved the

Code:
// Set session username to $by
$by = $_SESSION["phpAuth_username"];


now it says please enter a name

Code:
<?php

include "style.php";
$filename = 'link.txt'; // edit this
// Set session username to $by
$by = $_SESSION["phpAuth_username"];

function addlink($link_name,$link_url,$by,$email) {
global $filename;
$html = "<a href='$link_url' target='blank'>$link_name</a>, posted by <a href='mailto:$email'>$by\n";
$fp = fopen($filename, 'a+');


function logIP($link_name)
{
     $ipLog="log.txt"; // Your logfiles name here (.txt or .html extensions ok)


     $register_globals = (bool) ini_get('register_gobals');
     if ($register_globals) $ip = getenv(REMOTE_ADDR);
     else $ip = $_SERVER['REMOTE_ADDR'];

     $date=date ("l dS of F Y h:i:s A");
     $log=fopen("$ipLog", "a+");

     if (preg_match("/\bhtm\b/i", $ipLog) || preg_match("/\bhtml\b/i", $ipLog))
     {
          fputs($log, "IP: $ip - Link added: $link_name <br>");
     }
     else fputs($log, "IP: $ip - Link added: $link_name \n");

     fclose($log);
}
// Place the below function call wherever you want the script to fire.
logIp($link_name);


fputs($fp, $html) or die("Could not open file!");
echo 'The link was added successfully.<br><br>';

}

function showlinks() {
global $filename;
$html = implode('<br/>',array_reverse(file($filename)));
echo $html;
}

If (!$link_url){
   print "You must enter a Link Address";
} elseif (!$link_name){
   print "You must enter a Link Name";
} elseif (!$by){
   print "You must enter your Name";
} elseif (!$email){
   print "You must enter a Email Address";
} else {
   addlink($link_name,$link_url,$by,$email);
   print "<b>Existing Links</b>:<br>";    // adds new link
   showlinks();                      // display's list of links
}


?>
mathiaus
did you use the code I gave you because I removed that line but it's in the code you just posted Think
DanielXP
You wat Rolling Eyes Question
mathiaus
change the bottom part of the code to what I posted above
Code:
If (!$link_url){
   print "You must enter a Link Address";
} elseif (!$link_name){
   print "You must enter a Link Name";
} elseif (!$email){
   print "You must enter a Email Address";
} else {
   addlink($link_name,$link_url,$by,$email);
   print "<b>Existing Links</b>:<br>";    // adds new link
   showlinks();                      // display's list of links
}

That removes the checking for the name
DanielXP
it only posts this "MyLinkName, posted by "
DanielXP
Still not working some reson i can't get it to work Sad
BlackSkad
I could not figure out from your script where $by gets its value, but as that is the name of the one who posts the link, I changes it to the session var (at the end of the script):

Code:
if (!$link_url){
   print "You must enter a Link Address";
} elseif (!$link_name){
   print "You must enter a Link Name";
} elseif (!$by){
   print "You must enter your Name";
} elseif (!$email){
   print "You must enter a Email Address";
} else {
   addlink($link_name,$link_url,$_SESSION['phpAuth_username'], $email);   //CHANGED!
   echo '<b>Existing Links</b>:<br />';
   showlinks();
}
DanielXP
i don't understand Arrow Question
BlackSkad
EDIT: my mistake, seems like mathiaus already provided the correct solution, there's only a problem in your login. Seems like you don't get logged in correct, if you would, $by would not be empty.
mathiaus
Mad Why didn't I see this earlier???

Code:
session_start();

In your link adder page (at the top) Embarassed
DanielXP
mathiaus wrote:
Mad Why didn't I see this earlier???

Code:
session_start();

In your link adder page (at the top) Embarassed



What?
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.