View Full Version : Php
Markpyro
06-07-2005, 10:23 PM
I have one question:
After I have a form validated ( a login, basically), I want the page to redirect to a different one if the login succedes. Is there a function to do this?
-Mark
Fenguin
06-08-2005, 12:44 AM
header('Location: yourpage');
Markpyro
06-08-2005, 3:51 PM
Yes, but I thought it had to be at the beginning...
edit: fixed the old one.
<?php
$_Post["user"] = $user
$_Post["password"] = $pass
if (($user == "Geno" && $pass == "xxxx") || ($user == "Markpyro" && $pass == "xxxx"))
{
header('www.google.com');
}
else
{
header('fail.php');
}
?>
give me an error on line 5 which is "$_Post["password"] = $pass"
Black.Ice
06-08-2005, 5:37 PM
A few problems, Mark.
First of all, you had missing semi-colons at the end of the first two lines. That's why you were getting the error.
Second of all, you left out the location thing in the header. The script below will redirect properly. Make sure to include http://
Lastly, headers have to be done before any OUTPUT. Uncomment the echo statement to see the header fail. If I remember correctly, it should either give you an error or not redirect.
Let me know if this works.
<?php
//echo "Error. Uncomment to see no redirect";
$_Post["user"] = $user;
$_Post["password"] = $pass;
if (true)
{
header("Location: http://www.google.com");
}
else
{
header('Location: fail.php');
}
?>
Markpyro
06-08-2005, 6:12 PM
Warning: Cannot modify header information - headers already sent by (output started at c:\web\nextpageomg.php:3) in c:\web\nextpageomg.php on line 9
this is what I got. the script doesnt work.
why did you put "if(true)"?
Fenguin
06-08-2005, 7:12 PM
<?php
$user = $_POST["user"];
$pass = $_POST["password"];
if (($user == "Geno" && $pass == "xxxx") || ($user == "Markpyro" && $pass == "xxxx"))
{
header("Location: http://www.google.com");
}
else
{
header("Location: fail.php");
}
?>
Try the above. I'm not sure why you stored undefined variables into POST data; I just switched the order of $user and $_POST['user'].
Markpyro
06-08-2005, 7:57 PM
That worked, but when i enter my username and password from this form:
<html>
<body>
<p>
Please login
</P>
<form name="userpass" method="post" action="nextpageomg.php">
Username:
<input type="text" name="user"> <br>
Password:
<input type="password" name="password">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and change the "else" to 'echo "login failed";' in the code you've been editing
it always gives me a "login failed" message. ???
Black.Ice
06-08-2005, 10:39 PM
this is what I got. the script doesnt work.
why did you put "if(true)"?
I tested the code out on my server, and I didn't want to deal with the login. I changed it from true to false when I wanted to check both conditions. I overlooked your POST variable declarations... Fenguin caught them, which is why the script worked.
Now, I checked the page with the script and it's working fine. View an example here. (http://www.faisalm.com/php/login.php)
Here is the source for both files:
<?php
$user = $_POST["user"];
$pass = $_POST["password"];
if (($user == "Geno" && $pass == "xxxx") || ($user == "Markpyro" && $pass == "xxxx"))
{
header("Location: http://www.google.com");
}
else
{
echo "login failed";
}
?>
<html>
<body>
<p>
Please login
</P>
<form name="userpass" method="post" action="mark.php">
Username:
<input type="text" name="user"> <br>
Password:
<input type="password" name="password">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Markpyro
06-08-2005, 10:45 PM
heh, BI, sorry. I got it working before I saw your post. Unfourtunatly, I have more questions.
-what conditional would I use... err... with multiple conditions? for example- if user= 5, then do ... if user == 6, then do
like an "else if"
-is there somewhere I can register to get a free mysql database that I can remotely connect to? Mysql doesnt install on my old computer, but I need mysql to complete the rest of my project.
Black.Ice
06-08-2005, 10:51 PM
heh, BI, sorry. I got it working before I saw your post. Unfourtunatly, I have more questions.
-what conditional would I use... err... with multiple conditions? for example- if user= 5, then do ... if user == 6, then do
like an "else if"
-is there somewhere I can register to get a free mysql database that I can remotely connect to? Mysql doesnt install on my old computer, but I need mysql to complete the rest of my project.
I was never able to find a free mySQL Host.
About the multiple conditions, maybe a switch structure? With programming, there's a million ways to do the same thing. I would go for whatever is easier for you to understand. (I personally would probably end up writing a class or a function).
Markpyro
06-08-2005, 10:59 PM
Ahh. I tried switch, but i had to look at a tutorial because my book did not explain it. The concept is new to me. I tried using it, but php spurted out some wierd nonsensical jabber, and I ran away screaming :P
Anyways, I'll try again in the morning.
Graeme
06-09-2005, 3:03 PM
You could just use multiple elseif's for that, instead of doing a switch. I don't really see the purpose of using a switch in this instance.
On that note though, are you doing the elseif's for defining user access? Because if that's the case, it'd probably be easier to make a new column in the DB table for useraccess, where you can set access for many users at once. Ie.
DB Table:
userid | name | email | useraccess
<?php
if ($uaccess==1)
{
echo "Hello Admin.";
}
elseif($uaccess==2)
{
echo "Hello Moderator.";
}
else
{
echo "Hello Guest.";
}
?>
Black.Ice
06-09-2005, 3:34 PM
Ahh. I tried switch, but i had to look at a tutorial because my book did not explain it. The concept is new to me. I tried using it, but php spurted out some wierd nonsensical jabber, and I ran away screaming :P
Anyways, I'll try again in the morning.
Haha... Programming concepts are sometimes hard to get down, but when you fully understand them, they become really easy to implement.
Here's a switch structure.
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
You started off with the switch keyword. In the parenthesis, you include a variable. For simplicity, just think of the I as a number. The number stored in $i will execute the case number associated with it. If no case is equal to the number stored in $i, it will execute the information in the default case.
For example, if $i = 1, everything between case 1: and the break; will execute.
The variable $i can be anything though, it doesn't have to be a number. I sometimes use strings. For example
switch ($i) {
case "ice":
echo "i is Black.Ice";
break;
case "radlin":
echo "i is Radlin";
break;
case "markpyro":
echo "i is MarkPyro";
break;
default:
echo "i is no one of importance.";
}
Inside the $i variable, the choices would have to equal "ice", "radlin" or "markpyro" for it to work. Otherwise, it will echo the default statement.
The most common mistake I did was to forget the break; statement. Make sure you put it at the end of the case, or else it will execute until a break is found, or until the end of the switch.
I wrote this up really quickly, sorry if it's hard to follow. Let me know if you have any questions and I'll try to clarify.
Switch statements are nice for numbers or when you need fall through to occur, but I find them a bit messy for doing string checking. They're also error prone due to the aforementioned fall through behavior.
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.