Jump to content

If and else Statements -- Complete newbe, Please Help


Recommended Posts

Hi guys,

 

Please can anyone help me with a simple bit of PHP. I'm just starting out, so its all a bit confussing to me. Basically I need a bit of

code that does something like this:

 

My end user types in a password on a form on the html page.

 

The pasword is checked by the php page.

 

If pasword is correct the user gets sent to another page within my site.

 

If pasword is incorrect the user gets sent to an error page within my site.

 

 

I know this is real simple stuff, but as i said i'm realy new to this, hope some one can help.

 

Cheers People.

Here is some code that you can use to check for a 'hard coded' password, however you'd almost never use this in a real life situation, instead you would connect to a database and check the password against a stored one.

 

 

$rightPass = lucky;


//if a form has been submitted - otherwords, the user has submitted the password
if(isset ($_POST("submit"))){
if($_POST("password") == $rightPass){
  //user has entered the correct password.
  //either give them a hyperlink to the next page, or do this:
  header("next_page.php"):
}else{
//user has entered and incorrect password
echo "You have entered and incorrect password";
}

 

 

Hope that helps :)

 

 

Denno

Make sure the header is like this:

 

header("Location: http://www.example.com/example.php");
exit();

 

Make sure you exit out of the rest of the script.

 

Thanks. I was going straight from memory and I'm a bit of a novice myself, so couldn't remember exactly how it should have gone. Do you have to specify the full http link??

 

Denno

Wow, that was a quick response, thank you.

 

I'm still having a little trouble though, i'm getting a syntax error on the header lines. this is your code slightly modified for what i want.

 

<?php

 

$rightPass = ZF2011;

 

 

 

if(isset ($_POST("submit"))){

if($_POST("c") == $rightPass){

 

  header("success_page.php")} ****error on this line****

 

else{

header("error_page.php")} ****error on this line****

 

?>

 

 

Please can you let me know what i'm doing wrong?

 

Cheers,

Marcus.

Make sure the header is like this:

 

header("Location: http://www.example.com/example.php");
exit();

 

Make sure you exit out of the rest of the script.

 

That is how they show it over at php.net. Just to be safe I would do it that way.

 

Thanks. I was going straight from memory and I'm a bit of a novice myself, so couldn't remember exactly how it should have gone. Do you have to specify the full http link??

 

Denno

Thanks for helping guys but ive still got a problem, i get this message when i test the code:

 

Fatal error: Can't use function return value in write context in D:\Sites\mywebsite.com\form.php on line 16

 

This is the code i'm using:

 

<?php

 

$rightPass = ZF2011;

 

 

 

if(isset ($_POST("submit"))){

if($_POST("c") == $rightPass){

 

  header('Location: http://www.mywebsite.com/success.php');

exit();

}

 

else{

header('Location: http://www.mywebsite.com/error_page.php');

exit();

}

}

?>

Sure, no problem this is the code for the form.

 

<form id="form1" name="form1" method="post" action="form.php">

        <p>

          <label for="password"></label>

          <input name="password" type="text" id="c" maxlength="12" />

        </p>

        <p>

          <input type="submit" name="download" id="c" value="Download" />

        </p>

      </form>

 

Hope this helps you guys out. This is driving me mad :confused:

You need to change the if isset to have download in the double quotes, or c. I'm not sure exactly which one, but having submit in there does nothing. The reason I used submit is because it's commonly used as the name for the submit button, you've used download.

 

Denno

 

Cheers Denno, I changed the code as you advised but I still get this errror:

 

Fatal error: Can't use function return value in write context in D:\Sites\mywebsite.com\form.php on line 16

 

this is the HTML:

 

<form id="form1" name="form1" method="post" action="form.php">

        <p>

          <label for="password"></label>

          <input name="password" type="text" id="c" maxlength="12" />

        </p>

        <p>

          <input type="submit" name="download" id="c" value="Download" />

        </p>

      </form>

 

 

And this is the PHP:

 

<?php

 

$rightPass = ZF2011;

 

if(isset ($_POST("download"))){

if($_POST("c") == $rightPass){

 

  header("Location: http://www.mywebsite.com/success.php");

exit();

}

 

else{

header("Location: http://www.mywebsite.com/error_page.php");

exit();

}

}

?>

 

Cheers for sticking with us Denno mate, I thought you had it on your last post. God this is anoying. Hope we can find an answer.

Fixed!!! ..... well kind of. This was stressing me out big time, so i deleted the code i wrote and made an exact coppy of a simular thing that my work mate has used. Its not exactly what i wanted but i will make do with it. Thanks for all your help people, one day i hope php will actually make sense to me.

 

Cheers Guys,

 

Marcus.

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.