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
https://forums.phpfreaks.com/topic/107668-can-anyone-help-please/
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;
}
?>

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;
}
?>

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");
}
?>

 

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?

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?

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)

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");
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.