/    Sign up×
Community /Pin to ProfileBookmark

Hey in my site I have a client login form, and when the client logs in it takes them to a page called client.php which then shows their personalized data from my database. And I want this page to look exactly like the rest of my site, so where do I put in the html? Heres my script.

[CODE]<?php
// Connects to your Database
mysql_connect(“localhost”, “XXXXX”, “XXXXX”) or die(mysql_error());
mysql_select_db(“XXXXXX_XXXXXX”) or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE[‘ID_my_site’]))
{
$username = $_COOKIE[‘ID_my_site’];
$pass = $_COOKIE[‘Key_my_site’];
$check = mysql_query(“SELECT * FROM users WHERE username = ‘$username'”)or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info[‘password’])
{ header(“Location: index.php”);
}

//otherwise they are shown the admin area
else
{
echo “Admin Area<p>”;
echo “Your Content<p>”;
echo “<a href=index.php>Logout</a>”;
}
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header(“Location: index.php”);
}
?>[/CODE]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@GarySJun 29.2006 — Because you're using header information, you need to be careful not to output any HTML until after the header statements. Here's one way you could structure your page:

[code=html]
<?php
//the tests as per your posted example... without the echos

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>

<?php
echo "Admin Area<p>";
echo "Your Content<p>";
echo "<a href=index.php>Logout</a>";
?>


</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@GreyFilmProauthorJun 29.2006 — Ok so this is how my code looks now, is this right?

[CODE]<?php
// Connects to your Database
mysql_connect("localhost", "XXXXX", "XXXXX") or die(mysql_error());
mysql_select_db("XXXXXX") or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: index.php");
}

//otherwise they are shown the admin area
else
{
?>
<!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>Untitled Document</title>
<style type="text/css">
<!--
#background {
position:absolute;
left:0px;
top:0px;
width:854px;
height:640px;
z-index:0;
background-image: url(images/background.jpg);
}
#clientform {
position:absolute;
left:691px;
top:534px;
width:117px;
height:95px;
z-index:2;
}
#navbg {
position:absolute;
left:607px;
top:199px;
width:228px;
height:440px;
z-index:1;
background-image: url(images/nav_bg.png);
}
#navigation {
position:absolute;
left:613px;
top:198px;
width:217px;
height:301px;
z-index:3;
}
body {
background-image: url(images/canvas.jpg);
}
-->
</style>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<script type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
</script>
</head>
<?php
echo "Admin Area<p>";
echo "Your Content<p>";
echo "<a href=index.php>Logout</a>";
}
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: index.php");
}
?>
<div id="background" onload="MM_preloadImages('images/background.jpg')"></div>
<div id="clientform">
<form action="" method="post">
<table width="120" border="0" cellspacing="8" cellpadding="0">
<tr>
<td><label>
<input name="uname" type="text" id="uname" size="15" />
</label></td>
</tr>
<tr>
<td><label>
<input name="passwd" type="password" id="passwd" size="15" />
</label></td>
</tr>
<tr align=right>
<td><label>
<input type="submit" name="Submit" value="Login" />
</label></td>
</tr>
</table>
</form>
</div>
<div id="navbg"></div>
<div id="navigation">
<script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','217','height','301','src','flash/home nav','quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','flash/home nav' ); //end AC code
</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="217" height="301">
<param name="movie" value="flash/home nav.swf" />
<param name="quality" value="high" />
<param name="wmode" value="transparent" />
<embed src="flash/home nav.swf" width="217" height="301" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent"></embed>
</object>
</noscript></div>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@GarySJun 29.2006 — Almost there. Couple of things:
[list]
  • [*]Add an exit; after the header instruction on line 17 - to make sure the code on this page stops executing.

  • [*]The header line on line 91 won't work - this is a test that you need to make much higher up your code.

  • [/list]

    Moving it up should simplify the IFs and ELSEs: Because the headers will be followed by an "exit", there's no need for the HTML to be conditional:

    [code=html]
    <?php
    //pseudo code

    if ($condition1){
    header;
    exit;
    }
    if ($condition2){

    header;
    exit;
    }
    ?>
    <html>
    ....
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @GreyFilmProauthorJun 29.2006 — Im sort of confused...could you take the script I posted and arrange it how it should be for me?
    Copy linkTweet thisAlerts:
    @GarySJun 29.2006 — Hope I've intepreted what you're tring to do correctly:

    [code=html]
    <?php
    // Connects to your Database
    mysql_connect("localhost", "XXXXX", "XXXXX") or die(mysql_error());
    mysql_select_db("XXXXXX") or die(mysql_error());


    //CASE 1: NO COOKIE:
    if(!isset($_COOKIE['ID_my_site']){

    header("Location: index.php");
    exit;

    }

    //CASE 2: COOKIE
    //checks cookies to make sure they are logged in
    if(isset($_COOKIE['ID_my_site'])){
    $username = $_COOKIE['ID_my_site'];
    $pass = $_COOKIE['Key_my_site'];
    $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
    while($info = mysql_fetch_array( $check ))
    {

    //if the cookie has the wrong password, they are taken to the login page
    if ($pass != $info['password'])
    { header("Location: index.php");
    exit;
    }

    }

    //If the code gets this far, all must be OK


    ?>
    <!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>Untitled Document</title>
    <style type="text/css">
    <!--
    #background {
    position:absolute;
    left:0px;
    top:0px;
    width:854px;
    height:640px;
    z-index:0;
    background-image: url(images/background.jpg);
    }
    #clientform {
    position:absolute;
    left:691px;
    top:534px;
    width:117px;
    height:95px;
    z-index:2;
    }
    #navbg {
    position:absolute;
    left:607px;
    top:199px;
    width:228px;
    height:440px;
    z-index:1;
    background-image: url(images/nav_bg.png);
    }
    #navigation {
    position:absolute;
    left:613px;
    top:198px;
    width:217px;
    height:301px;
    z-index:3;
    }
    body {
    background-image: url(images/canvas.jpg);
    }
    -->
    </style>
    <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
    <script type="text/JavaScript">
    <!--
    function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
    }
    //-->
    </script>
    </head>
    <?php
    echo "Admin Area<p>";
    echo "Your Content<p>";
    echo "<a href=index.php>Logout</a>"; }
    ?>
    <div id="background" onload="MM_preloadImages('images/background.jpg')"></div>
    <div id="clientform">
    <form action="" method="post">
    <table width="120" border="0" cellspacing="8" cellpadding="0">
    <tr>
    <td><label>
    <input name="uname" type="text" id="uname" size="15" />
    </label></td>
    </tr>
    <tr>
    <td><label>
    <input name="passwd" type="password" id="passwd" size="15" />
    </label></td>
    </tr>
    <tr align=right>
    <td><label>
    <input type="submit" name="Submit" value="Login" />
    </label></td>
    </tr>
    </table>
    </form>
    </div>
    <div id="navbg"></div>
    <div id="navigation">
    <script type="text/javascript">
    AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','217','height','301','src','flash/home nav','quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','flash/home nav' ); //end AC code
    </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="217" height="301">
    <param name="movie" value="flash/home nav.swf" />
    <param name="quality" value="high" />
    <param name="wmode" value="transparent" />
    <embed src="flash/home nav.swf" width="217" height="301" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent"></embed>
    </object>
    </noscript></div>
    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @GreyFilmProauthorJun 29.2006 — What about the

    [CODE]//if the cookie does not exist, they are taken to the login screen
    {
    header("Location: home.php");
    } [/CODE]
    Copy linkTweet thisAlerts:
    @GarySJun 29.2006 — That's the condition I put right at the top of the code - line 6 or so?

    (In your previous post this condition redirected to index.php rather than home.php)

    As I said before, I might not have tested for the right things, but the principle is clear: do all the testing you need to do to get the "header redirect" stuff handled early.
    Copy linkTweet thisAlerts:
    @GreyFilmProauthorJun 29.2006 — Ok well I changed my script to the one you gave me and when I uploaded the client.php file onto my server and then viewed it it gave me a parse error on line 7, and then I uploaded the one I had before and it redircted me to the home.php page like it was supposed to.
    Copy linkTweet thisAlerts:
    @GarySJun 29.2006 — Yes - a missing ")" ... but did you really expect a "fully tested script" ?

    Must say I find the tone of your last post rather rude. Was it intended that way?
    Copy linkTweet thisAlerts:
    @GreyFilmProauthorJun 29.2006 — on no not at all, sorry I didn't mean for it to look that way, my bad.
    Copy linkTweet thisAlerts:
    @GreyFilmProauthorJun 29.2006 — hmm well now its not giving me the parse error but it isnt displaying anything, and at the bottom it says "Done, but with Errors on page" and it says theres something wrong with line 5 but theres nothing on line 5
    Copy linkTweet thisAlerts:
    @GarySJun 29.2006 — Thanks for that (... had some rough treatment from a client today... so I'm a bit touchy).

    Glad you got your script working.
    Copy linkTweet thisAlerts:
    @aussie_girlJun 30.2006 — hmm well now its not giving me the parse error but it isnt displaying anything, and at the bottom it says "Done, but with Errors on page" and it says theres something wrong with line 5 but theres nothing on line 5[/QUOTE]

    That usually means there is something wrong with your javascript
    ×

    Success!

    Help @GreyFilmPro spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...