Forum Moderators: coopster

Message Too Old, No Replies

PHP sessions

         

hal12b

1:04 pm on Jun 2, 2008 (gmt 0)

10+ Year Member



Hi - I am fairly new to PHP. I have many years of experience with Classic ASP and hate ASP.net, thus decided to go the PHP route.

First question of many to come :) -
In classic asp I could have a script similar to this (I am stripping out the basic code, so you can get the general idea for those not familiar with Classic ASP)

if username = "somevalue" then
session = 1
response.redirect "abc.html"
else
session = 2
response.redirect "error.html"

In PHP when I write a similar script, I get errors. The only redirect script I have found after many searches, works great if it is the only script on the top of the page.

Any advice is appreciated.
Thanks -
Hal

omoutop

1:24 pm on Jun 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try the following in the top of your page

<?PHP
session_start();
if (isset($_SESSION['mysession']) && $_SESSION['mysession']==1)
{
header("Location: somepage.htm");}
}
else
{
header("Location: errorpage.htm");
}
?>

the header redirect works only if no other output is send to the browser, thus put it on very top of page

hal12b

1:58 pm on Jun 2, 2008 (gmt 0)

10+ Year Member



Thanks for the reply, but what if I want to redirect based on a login?

for example, if the login is correct --> go to page1.htm

incorrect login --> go to error.htm

mehh

11:45 am on Jun 3, 2008 (gmt 0)

10+ Year Member



<?php 
$username = $_POST['USER'];
$passw = $_POST['PASSWORD'];
if ($username == "admin" && $passw=="pw"){
header("Location: /page1.html");
}
else{
header("Location: /page2.html");
}
?>

This isn't the best way to do it if you have alot of members, you probably wan't a MySql Database or something for that.

hal12b

6:32 pm on Jun 3, 2008 (gmt 0)

10+ Year Member



Thanks. I appreciate it.