Jump to content

Archived

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

Collymore

If Statement

Recommended Posts

Hi.

Basically I intend a user to log on to a site (MySQL). I want to create a simple table with two fields ('user' , 'number') and depending on what number is in the number field, I want the user to be directed to a certain page.

Can this be done by an IF statement on the login page? If so, what would the IF statement look like? There could be 50 different pages so there might be a more efficent way of doing this?

For example, if the number in the field is 22, then they will go to 22.php to interact with a table called 22.

Thanks in advance.

Share this post


Link to post
Share on other sites
[!--quoteo(post=382791:date=Jun 12 2006, 11:49 AM:name=Collymore)--][div class=\'quotetop\']QUOTE(Collymore @ Jun 12 2006, 11:49 AM) [snapback]382791[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi.

Basically I intend a user to log on to a site (MySQL). I want to create a simple table with two fields ('user' , 'number') and depending on what number is in the number field, I want the user to be directed to a certain page.

Can this be done by an IF statement on the login page? If so, what would the IF statement look like? There could be 50 different pages so there might be a more efficent way of doing this?

For example, if the number in the field is 22, then they will go to 22.php to interact with a table called 22.

Thanks in advance.
[/quote]

There may be more efficent ways of doing this but it really depends on the data they will be viewing once they have logged in.

If i was trying the above i would have a table with 3 columns [i]ie username password page[/i] in a table called users and have the form submitted to something like this.


[code]
<?php

$user = "username";
$pass = "password";
$host = "localhost";

$data = "database";

$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

mysql_connect($host, $user, $pass) or die(mysql_error());

mysql_select_db($data);

$report = mysql_query($query);

$num = mysql_num_rows($report);

if ($num == 1) {
     $row = mysql_fetch_array($report, MYSQL_ASSOC);
     extract($row);
         echo "<script language='javascript' type='text/javascript'>window.location.replace('path/page.php')</script>";
} else {
// Fail Script goes here...
}
?>
[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=382795:date=Jun 12 2006, 06:47 AM:name=scripts2go.co.uk)--][div class=\'quotetop\']QUOTE(scripts2go.co.uk @ Jun 12 2006, 06:47 AM) [snapback]382795[/snapback][/div][div class=\'quotemain\'][!--quotec--]
There may be more efficent ways of doing this but it really depends on the data they will be viewing once they have logged in.

If i was trying the above i would have a table with 3 columns [i]ie username password page[/i] in a table called users and have the form submitted to something like this.
[code]
<?php

$user = "username";

$pass = "password";
$host = "localhost";

$data = "database";

$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";

mysql_connect($host, $user, $pass) or die(mysql_error());

mysql_select_db($data);

$report = mysql_query($query);

$num = mysql_num_rows($report);

if ($num == 1) {
     $row = mysql_fetch_array($report, MYSQL_ASSOC);
     extract($row);
         echo "<script language='javascript' type='text/javascript'>window.location.replace('path/page.php')</script>";
} else {
// Fail Script goes here...
}
?>
[/code]
[/quote]


Thanks for the reply. They will not be viewing any data out of the tables but just running a script to update their records. As there is a progressive theme to the site, I need the site to know what stage the user is at.

Example.


A child is attempting to swim 5 metres, once he has completed 5 metres, the next week he will need to attempt a new target inputted by the parent. The teacher can then input if the target has been reached...and so on
The history of this needs to be recorded so that’s why I’m using more than one table rather than just deleting and replacing data out of one table.

I'm sure all this could be done with just 1 script rather than the dozens I'll have! but im afraid my PHP knowledge is quite poor! My main problem seems to be the site automatically knowing which week it needs to enter the data into with out asking the user.

I don't understand from your code how the 'page' field is manipulated?

Share this post


Link to post
Share on other sites
[!--quoteo(post=382791:date=Jun 12 2006, 05:49 AM:name=Collymore)--][div class=\'quotetop\']QUOTE(Collymore @ Jun 12 2006, 05:49 AM) [snapback]382791[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi.

Basically I intend a user to log on to a site (MySQL). I want to create a simple table with two fields ('user' , 'number') and depending on what number is in the number field, I want the user to be directed to a certain page.

Can this be done by an IF statement on the login page? If so, what would the IF statement look like? There could be 50 different pages so there might be a more efficent way of doing this?

For example, if the number in the field is 22, then they will go to 22.php to interact with a table called 22.

Thanks in advance.
[/quote]

login.php
[code]
<?php
session_start();

if ($_POST['login']) {
   if (!$_POST['user'] || !$_POST['pass']) {
      echo "no username or password entered.";
   } else {
      $conn = mysql_connect('localhost','username','password');
      $db = mysql_select_db('dbname',$conn);

      $user = mysql_real_escape_string($_POST['user']);
      $pass = mysql_real_escape_string($_POST['pass']);

      $sql = "select * from tablename where user = '$user' and pass = '$pass'";
      $result = mysql_query($sql);

      $is_user = mysql_num_rows($result);
      if ($is_user > 0) {
         $info = mysql_fetch_array($result);
         $_SESSION['userinfo'] = $info;
         header("location: targetpage".$info['page'].".php");
         exit();
      } else {
        echo "incorrect username or password.";
      }
   }
} else {
   $form = "<form action='{$_SERVER['PHP_SELF']}' method = 'post'>";
   $form.= "Name:<input type='text' name = 'user'><br>";
   $form.= "Password:<input type='password' name='pass'><br>";
   $form.= "<input type='submit' name = 'login' value='login'>";

   echo $form;
}
?>
[/code]

targetpage1.php (or whatever)
[code]
<?php
session_start();
if ($_SESSION['userinfo']) {
  $userinfo = $_SESSION['userinfo']);
} else {
   header("Location: login.php");
   exit();
}  

echo "hello ". $userinfo['user'] . " you are logged in and on page " . $userinfo['page'];

?>
[/code]

there are much more efficient ways to do your scripting though. for instance, if you have 50 pages that are all going to do about the same thing, you would want to have 1 page that just checks for which number to base the script off of, and you would do something like this:

header("Location: targetpage.php?page=".$info['page']);

which will send you to

targetpage.php?page=2

for example. and then you would check which page it is by something like so:

if ($_GET['page']) {
$page = $_GET['page'];
}

and then base your code on $page

Share this post


Link to post
Share on other sites
[!--quoteo(post=382825:date=Jun 12 2006, 08:47 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 12 2006, 08:47 AM) [snapback]382825[/snapback][/div][div class=\'quotemain\'][!--quotec--]
login.php
[code]
<?php
session_start();

if ($_POST['login']) {
   if (!$_POST['user'] || !$_POST['pass']) {
      echo "no username or password entered.";
   } else {
      $conn = mysql_connect('localhost','username','password');
      $db = mysql_select_db('dbname',$conn);

      $user = mysql_real_escape_string($_POST['user']);
      $pass = mysql_real_escape_string($_POST['pass']);

      $sql = "select * from tablename where user = '$user' and pass = '$pass'";
      $result = mysql_query($sql);

      $is_user = mysql_num_rows($result);
      if ($is_user > 0) {
         $info = mysql_fetch_array($result);
         $_SESSION['userinfo'] = $info;
         header("location: targetpage".$info['page'].".php");
         exit();
      } else {
        echo "incorrect username or password.";
      }
   }
} else {
   $form = "<form action='{$_SERVER['PHP_SELF']}' method = 'post'>";
   $form.= "Name:<input type='text' name = 'user'><br>";
   $form.= "Password:<input type='password' name='pass'><br>";
   $form.= "<input type='submit' name = 'login' value='login'>";

   echo $form;
}
?>
[/code]

targetpage1.php (or whatever)
[code]
<?php
session_start();
if ($_SESSION['userinfo']) {
  $userinfo = $_SESSION['userinfo']);
} else {
   header("Location: login.php");
   exit();
}  

echo "hello ". $userinfo['user'] . " you are logged in and on page " . $userinfo['page'];

?>
[/code]

there are much more efficient ways to do your scripting though. for instance, if you have 50 pages that are all going to do about the same thing, you would want to have 1 page that just checks for which number to base the script off of, and you would do something like this:

header("Location: targetpage.php?page=".$info['page']);

which will send you to

targetpage.php?page=2

for example. and then you would check which page it is by something like so:

if ($_GET['page']) {
$page = $_GET['page'];
}

and then base your code on $page
[/quote]

Thanks for the detail. I will give it a shot.

Share this post


Link to post
Share on other sites

×

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.