...

View Full Version : Different Content based on ID number



Hyp3r grunt
12-26-2007, 10:54 PM
I have a login script but regardless of the username and password you log in you'll always get the same results. I want to make it so the page that is included when you log in will do things based on your id number.

You go to stafflogin.html then you log in and it uses checklogin.html and then redirects you to loginsuccess.html where 'furtherbelowalternate.inc' is included. On all the other pages there is a thing that checks if you're logged in and if you are it includes 'furtherbelowalternate.inc' and if you're not it includes 'furtherbelow.inc'. Here are the pages:

stafflogin.html


<?php include('./above.inc'); ?>

<title>Website - Staff Login</title>

<?php include('./below.inc'); ?>

<div id="main">

<h1>Staff Login</h1>

<p class="standard"><form name="form1" method="post" action="checklogin.html">
Username: <input name="myusername" type="text" id="myusername">
<br>Password: <input name="mypassword" type="password" id="mypassword">
<br><input type="submit" name="Submit" value="Login"></form></p>

<?php include('./furtherbelow.inc'); ?>

checklogin.html


<?php
ob_start();
$host="mysql.x10hosting.com"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
$tbl_name="staff"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$encrypted_mypassword=md5($mypassword);

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

$query="SELECT id FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'";
$lesult=mysql_query($query);

$myid = "$lesult";

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
$count=mysql_num_rows($lesult);
// If result matched $myusername and $mypassword, table row must be 1 row

?>

<?php include('./above.inc'); ?>

<title>Website - Login Attempt Failed</title>

<?php include('./below.inc'); ?>

<div id="main">
<h1>Login Attempt Failed</h1>

<p><?php
if($count==1){
// Register $myusername, $mypassword and redirect to file "loginsuccess.html"
session_register("myusername");
session_register("mypassword");
session_register("myid");
header("location:loginsuccess.html");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush(); ?></p>
</div>

<?php include('./furtherbelow.inc'); ?>

loginsuccess.html


<?php
session_start();
if(!session_is_registered(myusername)){
header("location:stafflogin.html");
}
?>

<?php include('./above.inc'); $path = './'; ?>

<title>Website - Login Success</title>

<?php include('./below.inc'); ?>

<!-- Main Content Area -->

<div id="main">

<h1>Login Success</h1>

<?php include($path . 'topadvert.inc'); ?>

<p class="standard">Login Successful</p>

<?php include($path . 'bottomadvert.inc'); ?>

<!-- End of Main Content Area -->

<?php include('./furtherbelowalternate.inc'); ?>

furtherbelowalternate.inc


<?php
session_start();
?>

<div class="mainextension"></div>

</div>

<div id="rightbar">

<?php $path = './'; ?>

<div class="colsection">

<h1 class="sidecol">Feeds</h1>
<h3>RSS 2.0</h3>
<p class="feedsmenu"><a href="/feeds/rss/feed.xml">Feed</a></p>

</div>

<div class="colsection">

<h1 class="sidecol">Welcome <?php if ('$myid'=="1") {
echo "Hyp3r Grunt";
} else { echo "la"; } ?></h1>
<p class="standard"><img src="/images/avatars/avatar.gif" width="90" height="90" alt="Avatar" title="Avatar">
<br>What would you like to do?
<br> - <a href="/loginsuccess.html">Go Home</a>
<br> - <a href="/submitnews.html">Submit News</a>
<br> - <a href="/logout.html">Log out</a></p>

</div>

<div class="colsection">

<h1 class="sidecol">Statistics</h1>
<h3>News</h3>
<p class="standard">News Posts Submitted: <b><?php
include($path . 'dbconnectnews.php');
$query = "SELECT `id`
FROM `news`
WHERE `author` LIKE CONVERT( _utf8 '%Hyp3r Grunt%'
USING latin1 )
COLLATE latin1_swedish_ci
ORDER BY `id` DESC
LIMIT 0 , 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "{$row['id']}";
}
include($path . 'dbdisconnect.php');
?></b>
<br>News Posts Edited: <b>0</b>
<br>News Posts Deleted: <b>0</b></p>
<h3>Articles</h3>
<p class="standard">Articles Submitted: <b>0</b>
<br>Articles Edited: <b>0</b>
<br>Articles Accepted: <b>0</b></p>
<h3>Coding Problems</h3>
<p>Coding Problems Rectified: <b>0</b></p>

</div>


</div>

<div id="footer">

JUST FOOTER STUFF AND '</div>' TAGS BEYOND THIS POINT. THEN THERE IS THE FOLLOWING:

</body>
</html>

hammer65
12-27-2007, 03:30 PM
If you want different data per user, or different themes per user, you will need to either allow the user to set their preferences for this or assign them yourself, and then store those preferences in a database table. Use the user's unique id for their record in the users table (or whatever you are calling it) as a key in the preferences table to connect them to their view settings.

All you need is that id passed in a session variable, to retrieve those view preferences when needed anywhere on your site. That data can be alternate style sheets or some sort of "flag" to tell other database queries what data to retrieve.

Jesuspwnt
12-27-2007, 03:46 PM
sorry for being the idiot, but is it even possible to include php in a .html file..?

Hyp3r grunt
12-27-2007, 04:09 PM
I have an .htaccess file that has a line that makes lots of stuff happen, here is the line of code:


AddHandler application/x-httpd-phpv3 .html .inc .xml

This line makes it so I can use php in files with the extensions '.html', '.inc' and '.xml'.

Jesuspwnt
12-27-2007, 04:10 PM
ahh, see i knew it wasn't normally possible..
But the possibilities are endless with .htaccess :P

hammer65
12-27-2007, 04:16 PM
HTML doesn't have includes, but you can include HTML files in a PHP page. By the way, don't insert input values directly into a SQL query. Always use the escaping function appropriate to the database you are using (in this case mysql_real_escape_string()), or use prepared statements.

Hyp3r grunt
12-27-2007, 06:01 PM
I've made a page that is meant to create a table and insert information into that table but it isn't working. I usually use phpMyAdmin but it has died so I have to do it this way, here is the page I made:


<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('There was an error one');
$dbname = 'database';
mysql_select_db($dbname);

mysql_query("CREATE TABLE settings(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
username VARCHAR(65) NOT NULL DEFAULT '')")
or die ('There was an error two');

mysql_query("INSERT INTO settings VALUES ('1', 'Hyp3r Grunt')") or die ('There was an error three');

mysql_close($conn);
?>


Why doesn't it work?

Hyp3r grunt
12-27-2007, 06:32 PM
Okay I got my Create Table and Insert Information script working. Why isn't this working? I changed checklogin.html to this:


<?php
ob_start();
$host="mysql.x10hosting.com"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
$tbl_name="staff"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$encrypted_mypassword=md5($mypassword);

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

$statement="SELECT id FROM $result";
$lesult=mysql_query($statement);

$myid='$lesult';

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

?>

<?php include('./above.inc'); ?>

<title>Website - Login Attempt Failed</title>

<?php include('./below.inc'); ?>

<div id="main">
<h1>Login Attempt Failed</h1>

<p><?php
if($count==1){
// Register $myusername, $mypassword and redirect to file "loginsuccess.html"
session_register("myusername");
session_register("mypassword");
session_register("$myid");
header("location:loginsuccess.html");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush(); ?></p>
</div>

<?php include('./furtherbelow.inc'); ?>

I changed furtherbelowalternate.inc to this:


<?php
session_start();
?>

<div class="mainextension"></div>

</div>

<div id="rightbar">

<?php $path = './'; ?>

<div class="colsection">

<h1 class="sidecol">Feeds</h1>
<h3>RSS 2.0</h3>
<p class="feedsmenu"><a href="/feeds/rss/l.xml">News</a></p>

</div>

<div class="colsection">

<h1 class="sidecol">Welcome <?php include($path . 'dbconnectnews.php'); $gel="SELECT username FROM settings WHERE (myid) = '1'";
$hellol=mysql_query($gel); echo "$hellol"; include($path . 'dbdisconnect.php'); ?></h1>
<p class="standard"><img src="/images/avatars/goldelite.gif" width="90" height="90" alt="Avatar" title="Avatar">
<br>What would you like to do?
<br> - <a href="/loginsuccess.html">Go Home</a>
<br> - <a href="/submitnews.html">Submit News</a>
<br> - <a href="/submitarticle.html">Submit Article</a>
<br> - <a href="/logout.html">Log out</a></p>

</div>

<div class="colsection">

<h1 class="sidecol">Statistics</h1>
<p class="standard statsdivision">Personal</p>
<h3>News</h3>
<p class="standard">News Posts Submitted: <b><?php
include($path . 'dbconnectnews.php');
$query = "SELECT `id`
FROM `news`
WHERE `author` LIKE CONVERT( _utf8 '&#37;Hyp3r Grunt%'
USING latin1 )
COLLATE latin1_swedish_ci
ORDER BY `id` DESC
LIMIT 0 , 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "{$row['id']}";
}
include($path . 'dbdisconnect.php');
?></b>
<br>News Posts Edited: <b>0</b>
<br>News Posts Deleted: <b>0</b></p>
<h3>Articles</h3>
<p class="standard">Articles Submitted: <b>0</b>
<br>Articles Edited: <b>0</b>
<br>Articles Accepted: <b>0</b></p>
<h3>Coding Problems</h3>
<p>Coding Problems Rectified: <b>0</b></p>
<p class="standard statsdivision">Website</p>
<p class="standard">Pages: <b>55</b></p>

</div>


</div>

<div id="footer">
FOOTER STUFF AND '</div>' TAGS
</body>
</html>

In the 'furtherbelowalternate.inc' page I want it to say in between the '<h1>' tags 'Welcome (Whoever the user is here)'. Instead it doesn't give me anything and it just says 'Welcome'.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum