#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    6
    Rep Power
    0
    Hello,
    I have started to build my own site using php and mysql. I am using cookies to offer a greeting on the front page, and this works fine, however if someone is not registered I get the following error:
    Warning: Using scalar variable $row as an array or object in /www/barton-da/main.php3 on line 38
    In this part of the code it looks something like this:
    print "<td bgcolor="#0408A0" width="50%" height="100%" align="left"><font face="Verdana" size="1" color="#FFFFFF"><b>Welcome ";
    print "$row[first_name] $row[last_name]</b></td>";
    As I say, when someone is registered everything is fine but prior I get this problem, does some have an error handler to tell the page to leave these blank if there is no cookie?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    From your post, I'm assuming the value stored in the cookie is in some way used with a database connection to retrieve the user's first and last name....is this correct?

    Anyhow, the easiest way is to redirect the user to a "Not Registered" page in the absence/denial of cookies. If the cookie name is REGISTERED_USER, then you could put this above any HTML tags
    <?
    if(!$REGISTERED_USER)
    {
    header ("Location: not_registered.htm");
    exit();
    }
    ?>

    or, before your welcome message you could use
    <?
    if($REGISTERED_USER)
    {
    ...do the database call
    ...extract the information if it exists
    print"<B>Welcome $row[first_name] $row[last_name]";
    ?>

    To take it one step further security-wise, test the HTTP request method and pull the cookie value out of the $HTTP_COOKIE_VARS[] array before any HTML tags to defeat any GET method cookie hacks

    <?
    if(getenv("REQUEST_METHOD")=="GET")
    {
    $REGISTERED_USER="";
    $usercookie=$HTTP_COOKIE_VARS["REGISTERED_USER"];
    if(empty($usercookie))
    {
    header ("Location: not_registered.htm");
    exit();
    }
    }
    // at this point, the user has either been redirected to the alternate page or the cookie (presumably) exists on the client and you can go ahead and make your calls to the database to generate the welcome message using whatever pertinent value is stored in $usercookie. If this is a "POST" page, then you may have to do another branch of the outer IF statement to test other things - again, this is entirely dependent on your application

    ?>

    Hope I was clear about all this. Maybe I'm barking up the wrong tree????
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    6
    Rep Power
    0
    [QUOTE]Originally posted by Kyuzo:
    [B]From your post, I'm assuming the value stored in the cookie is in some way used with a database connection to retrieve the user's first and last name....is this correct?.....
    I didn't explain myself to well first time using this message board (new to PHP and all). Ok here is the top part of my code minus the user name and password

    <?php

    mysql_connect ("localhost", "username", "pass");

    mysql_select_db (dbname);

    $selectresult = mysql_query ("SELECT * FROM test
    WHERE USERID = '$CookieID'
    ");

    $row = mysql_fetch_array($selectresult);

    print "<html>";
    print "<head>";
    print "<title>Barton Design Associates Ltd. (Main Page)</title>";
    print "<link rel=STYLESHEET href="/style/mouse.css" type="text/css">";
    print "</head>";
    print " ";
    print "<body bgcolor="#000080" background="/images/back.jpg" text="#000000" link="#0000FF" alink="#0000FF" vlink="0000FF">";

    //Main page table set
    print "<center>";
    print "<table bgcolor="#000000" cellspacing="1" cellpadding="0" width="600">";
    print "<tr>";
    print "<td colspan="2" align="left" bgcolor="#000000" height="22"><font color="#FFFFFF" face="Arial" size="2"><b><a href="webh.php3" target="_self"> Web Hosting </a>|<a href="webd.php3" target="_self"> Web Design </a>|<a href="support.php3" target="_self"> Support </a>|<a href="contact.php3" target="_self"> Contact Us </a>|<a href="feed.php3" target="_self"> Feedback </a>|<a href="login.php3" target="_self"> Login </a>|<a href="reg.php3" target="_self"> Register</a></b></font></td>";
    print "</tr>";
    print "<tr>";
    print "<td align="left" bgcolor="#FFFFFF" height="63" width="166"><img src="/images/blogo.jpg"></td>";
    print "<td align="center" bgcolor="#FFFFFF" valign="center" height="63" width="100%"><img src="/images/banner.gif"></td>";
    print "</tr>";
    print "<tr>";
    print "<td colspan="2" bgcolor="#FFFFFF" height="18" width="100%">";

    //This is the bar with the name and date on
    print "<table bgcolor="#0408A0" cellspacing="0" cellpadding="2" height="18" width="100%">";
    print "<tr>";
    print "<td bgcolor="#0408A0" width="50%" height="100%" align="left"><font face="Verdana" size="1" color="#FFFFFF"><b>Welcome ";
    print "$row[first_name] $row[last_name]</b></td>";

    At this point if the user has not got an account I just want it to be left blank, and not refer them or anything fancy. But at the moment I get the error in my first message

    Also I know im a pain, but ideas/examples of how to allow me to create a login page for people who have say joined and not returned for a while and now their cookie has expired and gone. I have already taken the "username" and "pass" (for password) from them in the registration form.

    Thanks
  6. #4
  7. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    62
    Easy:
    <?
    if (isset($CookieID)) {

    mysql_connect ("localhost", "username", "pass");

    mysql_select_db (dbname);

    $selectresult = mysql_query ("SELECT * FROM test
    WHERE USERID = '$CookieID'
    ");

    $row = mysql_fetch_array($selectresult);

    }

    else {
    $row = array("" => "","" => ""); //building a null array
    }

    Now, if the cookie is set, the database query will run and create the firstname-lastname array, otherwise it will not run and the array will be created as NULL, thus nothing will print out except "Welcome"

    Another point about PHP: There is no need to print() all those lines of HTML, just:

    <HTML>etc....
    <?php
    PHP code here;
    ?>HTML code here <?php more PHP code here; ?>

    </HTML>
    (its called "sprinkling" PHP code inside HTML)

    For your users who have lost their cookies, there is no way to automatically detect that they were previous users, but you could always provide a link to a login page, (assuming you had them create a username and password when they signed up).
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Posts
    6
    Rep Power
    0
    Thanks, that did just the job

    The next part of my site I am doing is the login page as suggested, do you know of any examples I can use to help me?

    Thanks again for the help.

IMN logo majestic logo threadwatch logo seochat tools logo