Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 49
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Private messaging system

    I am including a PM system on my site and didn't want to write my own so I found one online, the problem I'm now having is that I need to tell it to pull the messages from the database based on the username save in the session but don't know how. The code below is what was put in the tutorial but it only selects based on the number entered.

    PHP Code:
    // Set the userid to 2 for testing purposes... you should have your own usersystem, so this should contain the userid
     
    $userid=2;
     
    // initiate a new pm class
     
    $pm = new cpm($userid); 

  • #2
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    This means that the code you found online checks on userid, not username.

    Rewriting this would involve changing the SQL query used, which means you need to find the __constructor for class cpm, find what it does with $userid, track down that function and change the SQL query from WHERE userid='{$userid}' into WHERE username='{username}' or something like that (and obviously change the variables $userid everywhere if you want to do it properly).

    After that's done, the rest should be plain sailing. Just replace $pm = new cpm($userid); with $pm = new cpm($_SESSION['username']; and you're there.

  • #3
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    I changed all occurrences of userid to username however I am now getting the error -
    Fatal error: Cannot redeclare cpm::getusername()

  • #4
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    Hmm... Did you replace all occurances? That would not be handy-dandy, as you would normally only need to change the query (providing the script is written that way).

    If you've done a straightforward 'replace userid by username', you'll now have two getusername functions which clashes, amongst a whole lot of other issues.

    Advice: reinstall the script and start afresh

    //edit
    Just realised that you probably misunderstood my comment "and obviously change the variables $userid everywhere if you want to do it properly". I meant it like this:

    Code:
    // Set the userid to 2 for testing purposes... you should have your own usersystem, so this should contain the userid
     $userid=2;
     // initiate a new pm class
     $pm = new cpm($userid);
    Becomes

    Code:
    // Set the userid to 2 for testing purposes... you should have your own usersystem, so this should contain the userid
     $username = $_SESSION['username'];
     // initiate a new pm class
     $pm = new cpm($username);
    Last edited by Thyrosis; 03-14-2013 at 06:46 PM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That indicates that class cpm has two methods called getUsername. PHP does not support overloading due to its datatype weakness, so you need to remove the the duplicate functions. If they are not the same, rename one or use logic switching based on the datatype of the provided argument within the function itself.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    These are the 2 pages the tutorial had me create, I hope someone can help.

    messages.php
    PHP Code:
    <?php

     
    /*************************************************************************
     *
     * Simple Private Messaging Tutorial for Pixel2Life Community
     * 
     * Features:
     * 
     * - Messaging using Usernames 
     * - No HTML allowed (bbcode can simply be included) 
     * - You can see if somebody has deleted or read the pm 
     * - On reply, the old mail will be quoted
     *
     * by Christian Weber
     * 
     * 
     *************************************************************************/
     
     // Load the config file!
     
    include('configmail.php');
     
    // Load the class
     
    require('pm.php');
     
    // Set the userid to 2 for testing purposes... you should have your own usersystem, so this should contain the userid
     
    $userid=1;
     
    // initiate a new pm class
     
    $pm = new cpm($userid);
     
     
    // check if a new message had been send
     
    if(isset($_POST['newmessage'])) {
       
    // check if there is an error while sending the message (beware, the input hasn't been checked, you should never trust users input!)
       
    if($pm->sendmessage($_POST['to'],$_POST['subject'],$_POST['message'])) {
         
    // Tell the user it was successful
         
    echo "Message successfully sent!";
       } else {
         
    // Tell user something went wrong it the return was false
         
    echo "Error, couldn't send PM. Maybe wrong user.";
       }
     }
     
     
    // check if a message had been deleted
     
    if(isset($_POST['delete'])) {
       
    // check if there is an error during deletion of the message
       
    if($pm->deleted($_POST['did'])) {
         echo 
    "Message successfully deleted!";
       } else {
         echo 
    "Error, couldn't delete PM!";
       }
     }
     
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Simple private Messaging</title>
    </head>
    <body>
    <?php
    // In this switch we check what page has to be loaded, this way we just load the messages we want using numbers from 0 to 3 (0 is standart, so we don't need to type this)
    if(isset($_GET['p'])) {
     switch(
    $_GET['p']) {
       
    // get all new / unread messages
       
    case 'new'$pm->getmessages(); break;
       
    // get all send messages
       
    case 'send'$pm->getmessages(2); break;
       
    // get all read messages
       
    case 'read'$pm->getmessages(1); break;
       
    // get all deleted messages
       
    case 'deleted'$pm->getmessages(3); break;
       
    // get a specific message
       
    case 'view'$pm->getmessage($_GET['mid']); break;
       
    // get all new / unread messages
       
    default: $pm->getmessages(); break;
     }
    } else {
     
    // get all new / unread messages
     
    $pm->getmessages();
    }
    // Standard links
    ?>
    <a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=new'>New Messages</a>
    <a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=send'>Send Messages</a>
    <a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=read'>Read Messages</a>
    <a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=deleted'>Deleted Messages</a>
    <br /><br />
    <?php
    // if it's the standart startpage or the page new, then show all new messages
    if(!isset($_GET['p']) || $_GET['p'] == 'new') {
    ?>
    <table border="0" cellspacing="1" cellpadding="1">
     <tr>
       <td>From</td>
       <td>Title</td>
       <td>Date</td>
     </tr>
     <?php
       
    // If there are messages, show them
       
    if(count($pm->messages)) {
         
    // message loop
         
    for($i=0;$i<count($pm->messages);$i++) {
           
    ?>
           <tr>
             <td><?php echo $pm->messages[$i]['from']; ?></td>
             <td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $pm->messages[$i]['id']; ?>'><?php echo $pm->messages[$i]['title'?></a></td>
             <td><?php echo $pm->messages[$i]['created']; ?></td>
           </tr>
           <?php
         
    }
       } else {
         
    // else... tell the user that there are no new messages
         
    echo "<tr><td colspan='3'><strong>No new messages found</strong></td></tr>";
       }
     
    ?>
    </table>
    <?php
    // check if the user wants send messages
    } elseif($_GET['p'] == 'send') {
    ?>

    <table border="0" cellspacing="1" cellpadding="1">
     <tr>
       <td>To</td>
       <td>Title</td>
       <td>Status</td>
       <td>Date</td>
     </tr>
     <?php
       
    // if there are messages, show them
       
    if(count($pm->messages)) {
         
    // message loop
         
    for($i=0;$i<count($pm->messages);$i++) {
           
    ?>
           <tr>
             <td><?php echo $pm->messages[$i]['to']; ?></td>
             <td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $pm->messages[$i]['id']; ?>'><?php echo $pm->messages[$i]['title'?></a></td>
             <td>
             <?php 
               
    // If a message is deleted and not viewed
               
    if($pm->messages[$i]['to_deleted'] && !$pm->messages[$i]['to_viewed']) {
                 echo 
    "Deleted without reading";
               
    // if a message got deleted AND viewed
               
    } elseif($pm->messages[$i]['to_deleted'] && $pm->messages[$i]['to_viewed']) {
                 echo 
    "Deleted after reading";
               
    // if a message got not deleted but viewed
               
    } elseif(!$pm->messages[$i]['to_deleted'] && $pm->messages[$i]['to_viewed']) {
                 echo 
    "Read";
               } else {
               
    // not viewed and not deleted
                 
    echo "Not read yet";
               }
             
    ?>
             </td>
             <td><?php echo $pm->messages[$i]['created']; ?></td>
           </tr>
           <?php
         
    }
       } else {
         
    // else... tell the user that there are no new messages
         
    echo "<tr><td colspan='4'><strong>No send messages found</strong></td></tr>";
       }
     
    ?>
    </table>

    <?php
    // check if the user wants the read messages
    } elseif($_GET['p'] == 'read') {
    ?>
     <table border="0" cellspacing="1" cellpadding="1">
     <tr>
       <td>From</td>
       <td>Title</td>
       <td>Date</td>
     </tr>
     <?php
       
    // if there are messages, show them
       
    if(count($pm->messages)) {
         
    // message loop
         
    for($i=0;$i<count($pm->messages);$i++) {
           
    ?>
           <tr>
             <td><?php echo $pm->messages[$i]['from']; ?></td>
             <td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $pm->messages[$i]['id']; ?>'><?php echo $pm->messages[$i]['title'?></a></td>
             <td><?php echo $pm->messages[$i]['to_vdate']; ?></td>
           </tr>
           <?php
         
    }
       } else {
         
    // else... tell the user that there are no new messages
         
    echo "<tr><td colspan='4'><strong>No read messages found</strong></td></tr>";
       }
     
    ?>
     </table>

    <?php
    // check if the user wants the deleted messages
    } elseif($_GET['p'] == 'deleted') {
    ?>
     <table border="0" cellspacing="1" cellpadding="1">
     <tr>
       <td>From</td>
       <td>Title</td>
       <td>Date</td>
     </tr>
     <?php
       
    // if there are messages, show them
       
    if(count($pm->messages)) {
         
    // message loop
         
    for($i=0;$i<count($pm->messages);$i++) {
           
    ?>
           <tr>
             <td><?php echo $pm->messages[$i]['from']; ?></td>
             <td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $pm->messages[$i]['id']; ?>'><?php echo $pm->messages[$i]['title'?></a></td>
             <td><?php echo $pm->messages[$i]['to_ddate']; ?></td>
           </tr>
           <?php
         
    }
       } else {
         
    // else... tell the user that there are no new messages
         
    echo "<tr><td colspan='4'><strong>No deleted messages found</strong></td></tr>";
       }
     
    ?>
    </table>
    <?php
    // if the user wants a detail view and the message id is set...
    } elseif($_GET['p'] == 'view' && isset($_GET['mid'])) {
     
    // if the users id is the recipients id and the message hadn't been viewed yet
     
    if($userid == $pm->messages[0]['toid'] && !$pm->messages[0]['to_viewed']) {
       
    // set the messages flag to viewed
       
    $pm->viewed($pm->messages[0]['id']);
     }
    ?>
     <table border="0" cellspacing="1" cellpadding="1">
       <tr>
         <td>From:</td>
         <td><?php echo $pm->messages[0]['from']; ?></td>
         <td colspan="2"></td>
       </tr>
       <tr>
         <td>Date:</td>
         <td><?php echo $pm->messages[0]['created']; ?></td>
         <td colspan="2"></td>
       </tr>
       <tr>
         <td>Subject:</td>
         <td colspan="3"><?php echo $pm->messages[0]['title']; ?></td>
       </tr>
       <tr>
         <td colspan="4"><?php echo $pm->render($pm->messages[0]['message']); ?></td>
       </tr>
     </table>
     <form name='reply' method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
       <input type='hidden' name='rfrom' value='<?php echo $pm->messages[0]['from']; ?>' />
       <input type='hidden' name='rsubject' value='Re: <?php echo $pm->messages[0]['title']; ?>' />
       <input type='hidden' name='rmessage' value='[quote]<?php echo $pm->messages[0]['message']; ?>[/quote]' />
       <input type='submit' name='reply' value='Reply' />
     </form>
     <form name='delete' method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
       <input type='hidden' name='did' value='<?php echo $pm->messages[0]['id']; ?>' />
       <input type='submit' name='delete' value='Delete' />
     </form>
    <?php
    }
    ?>
    <form name="new" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <strong>To:</strong>
    <input type='text' name='to' value='<?php if(isset($_POST['reply'])) { echo $_POST['rfrom']; } ?>' />
    <strong>Subject:</strong>
    <input type='text' name='subject' value='<?php if(isset($_POST['reply'])) { echo $_POST['rsubject']; } ?>' />
    <strong>Message:</strong><br />
    <textarea name='message'><?php if(isset($_POST['reply'])) { echo $_POST['rmessage']; } ?></textarea>
    <input type='submit' name='newmessage' value='Send' />
    </form>
    </body>
    </html>
    pm.php
    PHP Code:
    <?php
    /***********************************************************
    *
    * Simple Private Messaging Tutorial Class
    *
    ***********************************************************/

    class cpm {
     var 
    $username '';
     var 
    $messages = array();
     var 
    $dateformat '';

     
    // Constructor gets initiated with username
     
    function cpm($user,$date="d.m.Y - H:i") {
       
    // defining the given username to the classusername
       
    $this->username $user
       
    // Define that date_format
       
    $this->dateformat $date;
     }
     
     
    // Fetch all messages from this user
     
    function getmessages($type=0) {
       
    // Specify what type of messages you want to fetch
       
    switch($type) {
         case 
    "0"$sql "SELECT * FROM messages WHERE `to` = '".$this->username."' && `to_viewed` = '0' && `to_deleted` = '0' ORDER BY `created` DESC"; break; // New messages
         
    case "1"$sql "SELECT * FROM messages WHERE `to` = '".$this->username."' && `to_viewed` = '1' && `to_deleted` = '0' ORDER BY `to_vdate` DESC"; break; // Read messages
         
    case "2"$sql "SELECT * FROM messages WHERE `from` = '".$this->username."' ORDER BY `created` DESC"; break; // Send messages
         
    case "3"$sql "SELECT * FROM messages WHERE `to` = '".$this->username."' && `to_deleted` = '1' ORDER BY `to_ddate` DESC"; break; // Deleted messages
         
    default: $sql "SELECT * FROM messages WHERE `to` = '".$this->userid."' && `to_viewed` = '0' ORDER BY `created` DESC"; break; // New messages
       
    }
       
    $result mysql_query($sql) or die (mysql_error());
       
       
    // Check if there are any results
       
    if(mysql_num_rows($result)) {
         
    $i=0;
         
    // reset the array
         
    $this->messages = array();
         
    // if yes, fetch them!
         
    while($row mysql_fetch_assoc($result)) {
           
    $this->messages[$i]['id'] = $row['id'];
           
    $this->messages[$i]['title'] = $row['title'];
           
    $this->messages[$i]['message'] = $row['message'];
           
    $this->messages[$i]['fromid'] = $row['from'];
           
    $this->messages[$i]['toid'] = $row['to'];
           
    $this->messages[$i]['from'] = $this->getusername($row['from']);
           
    $this->messages[$i]['to'] = $this->getusername($row['to']);
           
    $this->messages[$i]['from_viewed'] = $row['from_viewed'];
           
    $this->messages[$i]['to_viewed'] = $row['to_viewed'];
           
    $this->messages[$i]['from_deleted'] = $row['from_deleted'];
           
    $this->messages[$i]['to_deleted'] = $row['to_deleted'];
           
    $this->messages[$i]['from_vdate'] = date($this->dateformatstrtotime($row['from_vdate']));
           
    $this->messages[$i]['to_vdate'] = date($this->dateformatstrtotime($row['to_vdate']));
           
    $this->messages[$i]['from_ddate'] = date($this->dateformatstrtotime($row['from_ddate']));
           
    $this->messages[$i]['to_ddate'] = date($this->dateformatstrtotime($row['to_ddate']));
           
    $this->messages[$i]['created'] = date($this->dateformatstrtotime($row['created']));
           
    $i++;
         }
       } else {
         
    // If not return false
         
    return false;
       }
     }
     
     
    // Fetch the username from a userid, I made this function because I don't know how you did build your usersystem, that's why I also didn't use left join... this way you can easily edit it
     
    function getusername($userid) {
       
    $sql "SELECT username FROM users WHERE `id` = '".$userid."' LIMIT 1";
       
    $result mysql_query($sql) or die(mysql_error());
       
    // Check if there is someone with this id
       
    if(mysql_num_rows($result)) {
         
    // if yes get his username
         
    $row mysql_fetch_row($result);
         return 
    $row[0];
       } else {
         
    // if not, name him Unknown
         
    return "Unknown";
       }
     }
     
     
    // Fetch a specific message
     
    function getmessage($message) {
       
    $sql "SELECT * FROM messages WHERE `id` = '".$message."' && (`from` = '".$this->userid."' || `to` = '".$this->userid."') LIMIT 1";
       
    $result mysql_query($sql);
       if(
    mysql_num_rows($result)) {
         
    // reset the array
         
    $this->messages = array();
         
    // fetch the data
         
    $row mysql_fetch_assoc($result);
         
    $this->messages[0]['id'] = $row['id'];
         
    $this->messages[0]['title'] = $row['title'];
         
    $this->messages[0]['message'] = $row['message'];
         
    $this->messages[0]['fromid'] = $row['from'];
         
    $this->messages[0]['toid'] = $row['to'];
         
    $this->messages[0]['from'] = $this->getusername($row['from']);
         
    $this->messages[0]['to'] = $this->getusername($row['to']);
         
    $this->messages[0]['from_viewed'] = $row['from_viewed'];
         
    $this->messages[0]['to_viewed'] = $row['to_viewed'];
         
    $this->messages[0]['from_deleted'] = $row['from_deleted'];
         
    $this->messages[0]['to_deleted'] = $row['to_deleted'];
         
    $this->messages[0]['from_vdate'] = date($this->dateformatstrtotime($row['from_vdate']));
         
    $this->messages[0]['to_vdate'] = date($this->dateformatstrtotime($row['to_vdate']));
         
    $this->messages[0]['from_ddate'] = date($this->dateformatstrtotime($row['from_ddate']));
         
    $this->messages[0]['to_ddate'] = date($this->dateformatstrtotime($row['to_ddate']));
         
    $this->messages[0]['created'] = date($this->dateformatstrtotime($row['created']));
       } else {
         return 
    false;
       }
     }
     
     
    // We need the userid for pms, but we only let users input usernames, so we need to get the userid of the username :)
     
    function getuserid($username) {
       
    $sql "SELECT id FROM users WHERE `username` = '".$username."' LIMIT 1";
       
    $result mysql_query($sql);
       if(
    mysql_num_rows($result)) {
         
    $row mysql_fetch_row($result);
         return 
    $row[0];
       } else {
         return 
    false;
       }
     }
     
     
    // Flag a message as viewed
     
    function viewed($message) {
       
    $sql "UPDATE messages SET `to_viewed` = '1', `to_vdate` = NOW() WHERE `id` = '".$message."' LIMIT 1";
       return (@
    mysql_query($sql)) ? true:false;
     }
     
     
    // Flag a message as deleted
     
    function deleted($message) {
       
    $sql "UPDATE messages SET `to_deleted` = '1', `to_ddate` = NOW() WHERE `id` = '".$message."' LIMIT 1";
       return (@
    mysql_query($sql)) ? true:false;
     }
     
     
    // Add a new personal message
     
    function sendmessage($to,$title,$message) {
       
    $to $this->getuserid($to);
       
    $sql "INSERT INTO messages SET `to` = '".$to."', `from` = '".$this->userid."', `title` = '".$title."', `message` = '".$message."', `created` = NOW()";
       return (@
    mysql_query($sql)) ? true:false;
     }
     
     
    // Render the text (in here you can easily add bbcode for example)
     
    function render($message) {
       
    $message strip_tags($message'');
       
    $message stripslashes($message); 
       
    $message nl2br($message);
       return 
    $message;
     }

    }

    ?>

  • #7
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    To be fair, that looks okay to me.

    What happens if you change $pm = new cpm($userid); by $pm = new cpm($_SESSION['username']);? Providing username is actually the session variable, and that this is stored as such in the 'to' column of the database that's being queried.

  • #8
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Doesn't do anything.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Its a bit antiquated, but it passes a lint test.
    That would indicate that this file does not match the file you are seeing the error from. You can post the full error message, but what you have so far indicates that you have a duplicate function signature within the class itself.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    The file posted above doesn't give an error but also doesn't give the correct information. It's when I change userid to username that I get the error.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What are you doing to change the userid to the username?

    Edit:
    If I understand you correctly, you're simply find and replacing userid to username by chance? If so, that breaks this:
    PHP Code:
     function getuserid($username) { 
    That would rename getUserid to getUsername($username). There is already a function that defines the getUsername($userid).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #12
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Yeah it sounds like thats what I'm doing.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Foster View Post
    Yeah it sounds like thats what I'm doing.
    Yep you can't do that.
    There is no need to rename anything here. The class already provides all the capabilities you need, just that the constructor doesn't allow a different type.
    In PHP 5+ OOP, you'd probably write two static methods, one that creates via username, and the other by userid. You *can* do this in PHP 4.x as well, but static binding is a pain. Effectively, you simply never refer to $this in any static context (since there is no static keyword [or I should say, I don't recall there being a static keyword in 4.x]).
    I'd switch the datatype. If its an integer, its an id, and if its a string, its a username.
    Without modifying the majority of the code in order to use determinative logic for whether its an id or name, you can sacrifice a single query and switch on the datatype provide. I would assume an integer is an id, and a string is a name.
    Post what the original CPM class code is.

    BTW, this is part of why storing an id is more valuable than a name. If you store your primary key field, any related records on that primary are much easier to find. If you store a username and its not the primary, than you need to resolve the primary key by either joining it in first, or by querying first.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #14
    Regular Coder
    Join Date
    Nov 2011
    Posts
    250
    Thanks
    8
    Thanked 4 Times in 4 Posts
    I've posted all the code that was in the tutorial.

    Would it be easier for me to store the id in the session along with the username? If so how would I add that? My login script is below.

    PHP Code:
    <?php

    include("config.php"); 

    // connect to the mysql server
    $link mysql_connect($server$db_user$db_pass)
    or die (
    "Could not connect to mysql because ".mysql_error());

    // select the database
    mysql_select_db($database)
    or die (
    "Could not select database because ".mysql_error());

    // username and password sent from form
    $username=$_POST['username'];
    $password=$_POST['password'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername stripslashes($username);
    $mypassword stripslashes($password);
    $myusername mysql_real_escape_string($username);
    $mypassword mysql_real_escape_string($password);

    $sql="SELECT * FROM users WHERE username='$username' and password='$password'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row

    if($count==1){

    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("username");
    session_register("password");
    header("location:myregiments.html");
    }
    else {
    echo 
    "Wrong Username or Password";
    }

  • #15
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    just add the id to session

    PHP Code:
    $row mysql_fetch_array($results);

    $_SESSION['UserID'] = $row['UserID']; 
    your script

    PHP Code:
    <?php
    session_start
    ();//added this line here
    include("config.php"); 

    // connect to the mysql server
    $link mysql_connect($server$db_user$db_pass)
    or die (
    "Could not connect to mysql because ".mysql_error());

    // select the database
    mysql_select_db($database)
    or die (
    "Could not select database because ".mysql_error());

    // username and password sent from form
    $username=$_POST['username'];
    $password=$_POST['password'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername stripslashes($username);
    $mypassword stripslashes($password);
    $myusername mysql_real_escape_string($username);
    $mypassword mysql_real_escape_string($password);

    $sql="SELECT * FROM users WHERE username='$username' and password='$password'";
    $result=mysql_query($sql);
    $row mysql_fetch_array($results);//added this line here

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row

    if($count==1){

    // Register $myusername, $mypassword and redirect to file "login_success.php"

    $_SESSION['UserID'] = $row['UserID'];//added this line here
    session_register("username");
    session_register("password");
    header("location:myregiments.html");
    }
    else {
    echo 
    "Wrong Username or Password";
    }

    and in your first script

    PHP Code:
     // Load the config file!
     
    include('configmail.php');
     
    // Load the class
     
    require('pm.php');

     
    $userid=$_SESSION['UserID'];//CHANGED this line here


     // initiate a new pm class
     
    $pm = new cpm($userid); 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •