Current location: Hot Scripts Forums » Programming Languages » PHP » View page through page


View page through page

Reply
  #1 (permalink)  
Old 10-02-07, 01:55 PM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
View page through page

Hi,

I have a Page B that should only be access through Page A as it deleted the user with the id next to it, if you access Page B now you cant do anything as there is no id but if you replace the id of the user you can delete the user. Page A is password protected but I need some kind of code to see if you have come from page A and not gone to page B directly

Thanks
Reply With Quote
  #2 (permalink)  
Old 10-02-07, 03:34 PM
Trevor's Avatar
Trevor Trevor is offline
Wannabe Coder
 
Join Date: Jun 2003
Location: Denver, Colorado
Posts: 120
Thanks: 0
Thanked 0 Times in 0 Posts
You can try 2 things.

First, try checking against the $_SERVER['HTTP_REFERER'] . If the value returned equals page A, let page b continue to opperate.

The second solution may be to use a session variable on page a, and check for it on page b. If it is not set, send the person back to page a.

I know these are just references... but I hope it helps.
__________________
The Universe Our God, Nature Our Temple, Love And Duty Our Religion, Knowledge Our Happiness And Consolation, Death The Dissolution Of The Ego, And The Return To Eternity.
Reply With Quote
  #3 (permalink)  
Old 10-02-07, 10:13 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If I were you and I wanted to password protect a delete function, I would
store the username and password in a couple of tables in an MySQL database

Create a table named "id_table" with two fields:
field name "id" as primary
field name "user"
In the user field store the username and
in the id field store a unique id.

Create a table named "pass_table with two fields:
field name "id" as primary
field name "pass"
In the id field store the same unique id as in the id_table
and in the pass field store the password.

and do something like this:

Page_a.php
PHP Code:

<?php
session_start
();
function 
error_message1()
{
 echo 
"<div style='width:383px;color:#ff0000;font-size:22;text-align:center;border:6px ridge #00ccff;padding:10px;padding-bottom:15px;background:#f0eeee;'>You have entered an incorrect username or password, please try again.</div>";
 }
function 
error_message2()
{
 echo 
"<div style='width:383px;color:#ff0000;font-size:22;text-align:center;border:6px ridge #00ccff;padding:10px;padding-bottom:15px;background:#f0eeee;'>You must enter your username and password, please try again.</div>";
 }
$user = isset($_POST["user"]) ? $_POST["user"] : "";
$pass = isset($_POST["pass"]) ? $_POST["pass"] : "";
if(
$user && $pass)
{
 
$conn=mysql_connect("host","username","password");
 
mysql_select_db("dbname");
 if(
$result mysql_query("select id from id_table where user = '".$user."'"))
 {
  while(
$row mysql_fetch_array($result))
  {
   
$id = isset($row["id"]) ? $row["id"] : "";

   }
  if(
$id)
  {
   if(
$result mysql_query("select pass from pass_table where id = '".$id."'"))
   {
    while(
$row mysql_fetch_array($result))
    {
     
$pass1 = isset($row["pass"]) ? $row["pass"] : "";
     if(
$pass1 == $pass)
     {
      include 
"code.php";
      
$_SESSION["logged_in"] = $session_code;
      
header("Location: http://www.example.com/page_b.php");
      }
     else
     {
      
error_message1();
      }
     }
    }
   else
   {
    
error_message1();
    }
   }
  }
 else
 {
  
error_message1();
  }
 }
if(isset(
$_POST["submit1"]) && !$user && !$pass){error_message2();}
?>
<html>
<head>
<style>
.delete
{
 width:383px;
 border:6px ridge #00ccff;
 padding:20px;
 background:#aabbcc;
 color:#0000ff;
 }
.center
{
 text-align:center;
 }
.formatting1
{
 font-size:26px;
 font-weight:bold;
 }
.formatting2
{
 font-size:18px;
 font-weight:bold;
 color:#aa5500;
 }
.span1
{
 width:280px;
 text-align:right;
 font-size:18px;
 font-weight:bold;
 color:#008855;
 }
</style>
</head>
<body>
<div class="delete">
<form name="theForm" action="#" method="POST">
<div class="center formatting1">To delete user, enter username & password.</div>
<p>
<span class="span1">UserName : <input type="text" name="user"></span>
<p>
<span class="span1">Password : <input type="password" name="pass"></span>
<p><br />
<div class="center"><input class="formatting2" type="submit" name="submit1" value="Submit"></div>
</form>
</div>
</body>
</html>
And on page_b.php I would do something like this:
PHP Code:

<?php
session_start
();
include 
"code.php";
if(isset(
$_SESSION["logged_in"]) && $_SESSION["logged_in"] == $session_code)
{
 
// Rest of program here.
 
}
else
{
 
header("Location: http://www.example.com/page_a.php");
 }
?>
And code.php: Note, it would be more secure if you stored this value in your database.
PHP Code:

<?php
$session_code 
"a135B6c396";
?>
__________________
Jerry Broughton

Last edited by job0107; 10-02-07 at 10:41 PM.
Reply With Quote
  #4 (permalink)  
Old 10-02-07, 11:08 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Correction to page_a.php

Sorry I left out one else statement and one last check.

page_a.php
PHP Code:

<?php
session_start
();
function 
error_message1()
{
 echo 
"<div style='width:383px;color:#ff0000;font-size:22;text-align:center;border:6px ridge #00ccff;padding:10px;padding-bottom:15px;background:#f0eeee;'>You have entered an incorrect username or password, please try again.</div>";
 }
function 
error_message2()
{
 echo 
"<div style='width:383px;color:#ff0000;font-size:22;text-align:center;border:6px ridge #00ccff;padding:10px;padding-bottom:15px;background:#f0eeee;'>You must enter your username and password, please try again.</div>";
 }
$user = isset($_POST["user"]) ? $_POST["user"] : "";
$pass = isset($_POST["pass"]) ? $_POST["pass"] : "";
if(
$user && $pass)
{
 
$conn=mysql_connect("host","username","password");
 
mysql_select_db("dbname");
 if(
$result mysql_query("select id from id_table where user = '".$user."'"))
 {
  while(
$row mysql_fetch_array($result))
  {
   
$id = isset($row["id"]) ? $row["id"] : "";
   }
  if(
$id)
  {
   if(
$result mysql_query("select pass from pass_table where id = '".$id."'"))
   {
    while(
$row mysql_fetch_array($result))
    {
     
$pass1 = isset($row["pass"]) ? $row["pass"] : "";
     if(
$pass1 == $pass)
     {
      include 
"code.php";
      
$_SESSION["logged_in"] = $session_code;
      
header("Location: http://www.example.com/page_b.php");
      }
     else
     {
      
error_message1();
      }
     }
    }
   else
   {
    
error_message1();
    }
   }
  else
  {
   
error_message1();
   }
  }
 else
 {
  
error_message1();
  }
 }
if(isset(
$_POST["submit1"]) && ($user && !$pass || !$user && $pass)){error_message1();}
if(isset(
$_POST["submit1"]) && !$user && !$pass){error_message2();}
?>
<html>
<head>
<style>
.delete
{
 width:383px;
 border:6px ridge #00ccff;
 padding:20px;
 background:#aabbcc;
 color:#0000ff;
 }
.center
{
 text-align:center;
 }
.formatting1
{
 font-size:26px;
 font-weight:bold;
 }
.formatting2
{
 font-size:18px;
 font-weight:bold;
 color:#aa5500;
 }
.span1
{
 width:280px;
 text-align:right;
 font-size:18px;
 font-weight:bold;
 color:#008855;
 }
</style>
</head>
<body>
<div class="delete">
<form name="theForm" action="#" method="POST">
<div class="center formatting1">To delete user, enter username & password.</div>
<p>
<span class="span1">UserName : <input type="text" name="user"></span>
<p>
<span class="span1">Password : <input type="password" name="pass"></span>
<p><br />
<div class="center"><input class="formatting2" type="submit" name="submit1" value="Submit"></div>
</form>
</div>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 10-02-07 at 11:29 PM.
Reply With Quote
  #5 (permalink)  
Old 10-03-07, 07:59 AM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
page_a.php works thanks, but page_b.php does not I have done this


PHP Code:



// Start the session
session_start();

// Define $session_code
$session_code "ghr57g834f";

if(isset(
$_SESSION["logged_in"]) && $_SESSION["logged_in"] == $session_code)
{

////////
/////Rest of the page code here
///////

}
else
{
 
header("Location: /page_a.php");
 } 
When I visit page_b.php directly I see the page, it should direct me to page_a.php but it doesnt any ideas?

Thanks
Reply With Quote
  #6 (permalink)  
Old 10-03-07, 08:07 AM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
I fixed this the session was still open when tried in a different browser and when session_close(); was done it worked. Thanks alot
Reply With Quote
  #7 (permalink)  
Old 10-03-07, 08:50 AM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
I have a problem I need some kind of logout script when i do page_b.php i can view it without it redirecting me to page_a.php why is it because the session is still remember so I need to close it some how?

I done this

PHP Code:

session_start();  


unset(
$_SESSION['logged_in']);

echo 
"logged out"
Then I go and view page_b.php and it redirects me to page_a.php with an error

Notice: A session had already been started - ignoring session_start() on page_a.php

So it all would work if I get rid of the error

Thanks

Last edited by TheCase; 10-03-07 at 09:06 AM.
Reply With Quote
  #8 (permalink)  
Old 10-03-07, 09:11 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Try adding one more line to page_b.php:

page_b.php
PHP Code:

<?php
session_start
(); 
$session_code "ghr57g834f"
if(isset(
$_SESSION["logged_in"]) && $_SESSION["logged_in"] == $session_code

 
$_SESSION["logged_in"] = "";
 
//////// 
 /////Rest of the page code here 
 /////// 
 

else 

 
header("Location: /page_a.php"); 
 }
?>
__________________
Jerry Broughton
Reply With Quote
  #9 (permalink)  
Old 10-03-07, 09:37 AM
TheCase TheCase is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Ok I am getting closer now, it is logging me out and everything so when I log out and go to page_b it echo's a message "You need to log in " then bellow that i include the page_a.php but that error message is in the way if that was gone it would work. Tryed what you suggested just added more erriors

Quote:
You need to enter the password
Notice: A session had already been started - ignoring session_start() in page_a
Followed underneath by page_a.php
Thanks
Reply With Quote
  #10 (permalink)  
Old 10-03-07, 09:50 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I need to see your code. But somewhere you have session_start(); twice
when you only need it once.
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Image check - view page vivabensmith PHP 1 09-07-07 02:03 AM
[turnkey] Lyrics site - $149 (LEGAL) rightinpoint General Advertisements 0 10-22-06 04:33 AM
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
page browsing problem mivec PHP 3 04-17-04 03:43 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM


All times are GMT -5. The time now is 06:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.