Jump to content

Can ANYONE Help...? PLease??


Pavlos1316

Recommended Posts

Hi

 

This code is for authendicate user. It works fine when I put it in members.index. But if I paste it in my other restricted pages when i log in and try to access them... i get the unauthorized msg

 

<?php
//members pagesession_start();
if (!isset ($_SESSION['Username']) OR empty ($_SESSION['Username'])){
//Unauthorized page
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="refresh" content="2;url=/">
</head>
<body>
<center><h1>Unauthorized Access!</h1></center>
<center><h1>Please Register... It is TOTALLY Free!!!</h1></center>
</body>
</html>
<?php
exit;
}
?>

 

Link to comment
Share on other sites

proper code would be

<?php
//members pagesession_start();
if (!isset ($_SESSION['Username']) || !isset(($_SESSION['Username'])){
//Unauthorized page
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="refresh" content="2;url=/">
</head>
<body>
<center><h1>Unauthorized Access!</h1></center>
<center><h1>Please Register... It is TOTALLY Free!!!</h1></center>
</body>
</html>
<?php
exit;
}
?>

Link to comment
Share on other sites

got trigger happy with the (

(accidentally put one too many in when re-working the code)

<?php
//members pagesession_start();
if (!isset ($_SESSION['Username']) || !isset($_SESSION['Username'])){
//Unauthorized page
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="refresh" content="2;url=/">
</head>
<body>
<center><h1>Unauthorized Access!</h1></center>
<center><h1>Please Register... It is TOTALLY Free!!!</h1></center>
</body>
</html>
<?php
exit;
}
?>

Link to comment
Share on other sites

Is not dub cause I am quite new at this. I don't know If I should add a code lets say in login.php to remember that you are logged in

 

If you are talking about my login.php here it is

 

mysql_connect($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname)or die(mysql_error());session_start();
$Username=$_POST['Username'];
$Password=$_POST['Password'];
$query="SELECT * FROM Register WHERE Username='$Username' and Password='$Password'";
$result=mysql_query($query);
if(mysql_num_rows($result)!=1){
$error="Invalid Login";
include ("main.html");
echo "<center><font color=#FF0000>Invalid Login</font></center>";
}else{
$_SESSION['Username']="$Username";
include ("members.html");
}
?>

 

Link to comment
Share on other sites

cleaned up the code (adding security if you want it)

<?php
mysql_connect($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname)or die(mysql_error());session_start();
$Username=mysql_real_escape_string($_POST['Username']);
$Password=mysql_real_escape_string($_POST['Password']);
$query="SELECT * FROM Register WHERE Username='$Username' and Password='$Password'";
$result=mysql_query($query);
if(mysql_num_rows($result)!=1){
$error="Invalid Login";
include ("main.html");
echo "<center><font color=#FF0000>Invalid Login</font></center>";
}else{
$_SESSION['Username']=$Username;
include ("members.html");
}
?>

ok, so, can we see the code for the pages that aren't keeping track of if the user is logged in?

Link to comment
Share on other sites

it is welcome ;)

 

Is quite long code.. 90 lines. But if you need it I will post it.

 

Just to know is plain html code plus my authentication script you just modified paste at the begining.

Just like my members page that is working.

 

You need it?

 

Oh and did you remove " " from $Username from the 4th line from the end of my code or was accidentaly?

Link to comment
Share on other sites

starting off from square one, so there isn't any misunderstanding, this is a basic example on how I would do it:

login.php

<?php
session_start();
mysql_connect($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname)or die(mysql_error());
$Username=mysql_real_escape_string($_POST['Username']);
$Password=mysql_real_escape_string($_POST['Password']);
$query="SELECT * FROM Register WHERE Username='$Username' and Password='$Password'";
$result=mysql_query($query);
if(mysql_num_rows($result)!=1){
$error="Invalid Login";
include ("main.html");
echo "<center><font color=#FF0000>Invalid Login</font></center>";
}else{
$_SESSION['Username']=$Username;
include ("members.html");
}
?>

secure page

<?php
session_start();
//members pagesession_start();
if (!isset ($_SESSION['Username'])){
//Unauthorized page
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="refresh" content="2;url=/">
</head>
<body>
<center><h1>Unauthorized Access!</h1></center>
<center><h1>Please Register... It is TOTALLY Free!!!</h1></center>
</body>
</html>
<?php
exit;
}
?>

holy cow. does your members.html contain your php? if so, there's your problem.  php is only executed server-side if your server knows to execute it as such. If it's in an html file, the server won't consider to parse through it for php (unless your server is set up to read HTML files as PHP)

Link to comment
Share on other sites

Still doesn't work for the rest protected pages. (I am stuck here 20 days now!!!)

 

How do I fix that????

 

members.html:

 

<?php
session_start();
//members page
if (!isset ($_SESSION['Username'])){
//Unauthorized page
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="refresh" content="2;url=/">
</head>
<body>
<center><h1>Unauthorized Access!</h1></center>
<center><h1>Please Register... It is TOTALLY Free!!!</h1></center>
</body>
</html>
<?php
exit;
}
?>
html code here

 

 

login.php

 

<?php
session_start();
//Database Information
$dbhost="localhost";
$dbname="data";
$dbuser="username";
$dbpass="password";
//Connect to database
mysql_connect($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname)or die(mysql_error());
$Username=mysql_real_escape_string($_POST['Username']);
$Password=mysql_real_escape_string($_POST['Password']);
$query="SELECT * FROM Register WHERE Username='$Username' and Password='$Password'";$result=mysql_query($query);
if(mysql_num_rows($result)!=1){
$error="Invalid Login";
include ("main.html");
echo "<center><font color=#FF0000>Invalid Login</font></center>";
}else{
$_SESSION['Username']=$Username;
include ("members.html");
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.