Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4830
  • Last Modified:

Passing Variables to another page after Redirecting a Page

Hi,
  I have made a script that basically is a login page and authenticates a user, then redirects them to another page. Problem I am having now, is that I need a couple of variables passed to the other page so that I can call certain functions, here is the part of my script that does the redirect:

if ($num !=0)
            {
                  $auth=true;            
                  $user_level = $rows[2];
                  printf("<table align =center><tr><td><FONT Color ='Blue'><strong>Successful Login, You will be redirected in approx 2 Seconds</strong></FONT></td></tr>");
                  printf("<META HTTP-EQUIV='refresh'CONTENT='2;URL=main.php'</table>");
            }

Is there a better way to do this? Thanks.
0
Zambian4eva
Asked:
Zambian4eva
  • 10
  • 9
  • 3
  • +1
1 Solution
 
shmertCommented:
Use the header() function to do your redirects.  Then there is no delay.

As for passing parameters, you should be able to include a GET string in the URL, for either method:

printf("<META HTTP-EQUIV='refresh'CONTENT='2;URL=main.php?value1=foo&value2=this+has+spaces'</table>");

    - OR -

header("Location: main.php?value1=foo&value2=this+has+spaces");

Note that if your args have weird characters or spaces, you'll want to call urlencode() on them first.
0
 
shmertCommented:
One last head up:  if you use header() you need to call it BEFORE any whitespace or content is output by your script.  Using ob_start() at the top of your page is a good way to prevent this from happening accidentally.
0
 
Zambian4evaAuthor Commented:
Is there any way to pass them without displaying them? Because I don't those values to be seen by the end user.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
lozlozCommented:
register the values in a session:

<?
$data = "sensitive";
session_register("data");
$this = "is very interesting";
session_register("this");
header("Location: private.php");
exit;
?>

then call them with $_SESSION["data"] etc.
0
 
shmertCommented:
Yup, session is your best bet.  Just be sure to call session_start().  You can also register session variables more easily by just setting them in the $_SESSION array:

<?php
session_start();
$password = $_POST['password'];
$_SESSION['userpassword'] = $password;
header("Location: otherpage.php");
?>

then call session_start() on all your pages, and you can reference the $_SESSION data.
0
 
Zambian4evaAuthor Commented:
I appreciate the help, still can't get it to work right, I am including the index.php page script and then the header(location: ird.php) page script to. Hopefully you can tell me what I am doing wrong:

Index.php
            if ($num !=0)
            {
                  $user_id=$rows[0];
                  echo $user_id;
                  $user_pass=$rows[1];
                  printf("Success");
                  session_start();
                  $_SESSION['username'] = $user_id;
                  echo $_SESSION['username'];
                  $_SESSION['userpassword'] = $user_pass;
                  header("Location: ird.php");
                           }


IRD.php

<?php
session_start();
echo $_SESSION["username"];
?>

I can't get it to display the variable passed to the ird.php so that I can display data depending on the login information.

Once again, Thanks for the all the help.
0
 
lozlozCommented:
you need session_start(); at the top of your page on index.php
0
 
Zambian4evaAuthor Commented:
okay, I put session_start(); right under the <?php on the first line, this however still does not seem to work. Is there anything I need to check to see if the session is actually setting? Cause the $_SESSION['username'] is getting set on the index.php page. Here is the entire index.php script:

<head>
      <title>Web Login</title>
</head>
<body>
<?php
session_start();

printf("<table align=center>
<tr><td><STRONG><FONT size=4>Enter Username and Password</td></tr>
<form enctype='multipart/form-data' method='post' action='$PHP_SELF'>
<tr><td align =center><STRONG><FONT size=3>USERNAME: </FONT><input type ='text' maxLength=8 size=8 name='username' class='textbox'></td></tr>
<tr><td align=center><STRONG><FONT size=3>PASSWORD: </FONT><input type ='password' maxLength=8 size=8 name='user_pass' class='textbox'></td></tr>
<tr><td align=center><Input Type = 'submit' name='submit_login' value='Login'>
</form></table><br><hr size='1'><br>");

if($submit_login)
{
             $db = mysql_connect("", "test","work");
        mysql_select_db("booyaa",$db);
        $sql = "select username, user_passwd, user_level from users where username = '$username' and user_passwd = '$user_pass'";
            $result = mysql_query($sql);
        $num = mysql_numrows($result);
            $rows = mysql_fetch_array($result);
            if ($num !=0)
            {
                  $auth=true;
                  $user_id=$rows[0];
                  echo $user_id;
                  $user_pass=$rows[1];
            //      printf("Success");
                  session_start();
                  $_SESSION['username'] = $user_id;
            //      echo $_SESSION['username'];
                  $_SESSION['userpassword'] = $user_pass;
                  header("Location: ird.php");
                  exit;
            }
            if(!$auth)
            {
            printf("<table align =center><td><FONT Color='RED'><strong>LOGIN DENIED</strong></td></table>");
            }
}

?>
</body>
</html>

Thanks again.


0
 
lozlozCommented:
you actually need the session_start(); right before any output, so before the html

the first two lines need to be
<?
session_start();

then you have
?>
<head>
     <title>Web Login</title>
</head>
<body>

etc..
0
 
Zambian4evaAuthor Commented:
still not getting passed to ird.php. Is my logic even correct? Cause obviously nothing is getting passed. Here is ird.php:

<?php
session_start();
?>
<?php
echo $_SESSION['userpassword'];
?>

Is this correct? Cause if all this is correct, I can't seem to understand why the variable is not getting passed?
0
 
lozlozCommented:
maybe do a simple test, e.g.

page1.php:

<?
session_start();
$var1 = "test";
session_register("var1");
$_SESSION["var2"] = "test";
header("Location: page2.php");
exit;
?>

page2.php:

<?
session_start();
print "session var: <br />\n";
print_r($_SESSION);
?>

tell me what the results are
0
 
Zambian4evaAuthor Commented:
session var:


That's all I get on page2.php. For some reason it isn't carrying the sessions over. Is there anything I should check in the php.ini file?

Cause I just tried it with another session test script and the sessionID carried over to page2.php but the variables didn't.
0
 
Zambian4evaAuthor Commented:
This is how the php.ini file was setup by the person who left:

[Session]
session_start              = 1
session.save_handler      = files   ; handler used to store/retrieve data
session.save_path         = C:\WINDOWS\Temp    ; argument passed to save_handler
                                    ; in the case of files, this is the
                                    ; path where data files are stored
session.use_cookies       = 0       ; whether to use cookies
session.name              = PHPSESSID  
                                    ; name of the session
                                    ; is used as cookie name
session.auto_start        = 0       ; initialize session on request startup
session.cookie_lifetime   = 0       ; lifetime in seconds of cookie
                                    ; or if 0, until browser is restarted
session.cookie_path       = /       ; the path the cookie is valid for
session.cookie_domain     =         ; the domain the cookie is valid for
session.serialize_handler = php     ; handler used to serialize data
                                    ; php is the standard serializer of PHP
session.gc_probability    = 1       ; percentual probability that the
                                    ; 'garbage collection' process is started
                                    ; on every session initialization
session.gc_maxlifetime    = 1440    ; after this number of seconds, stored
                                    ; data will be seen as 'garbage' and
                                    ; cleaned up by the gc process
session.referer_check     =         ; check HTTP Referer to invalidate
                                    ; externally stored URLs containing ids
session.entropy_length    = 0       ; how many bytes to read from the file
session.entropy_file      =         ; specified here to create the session id
; session.entropy_length    = 16
; session.entropy_file      = /dev/urandom
session.cache_limiter     = nocache ; set to {nocache,private,public} to
                                    ; determine HTTP caching aspects
session.cache_expire      = 180     ; document expires after n minutes
session.use_trans_sid     = 1       ; use transient sid support if enabled
                                    ; by compiling with --enable-trans-sid

0
 
lozlozCommented:
what version of php are you using incidentally
0
 
Zambian4evaAuthor Commented:
4.0.4
0
 
Zambian4evaAuthor Commented:
It is running on apache...
0
 
lozlozCommented:
i dont know why session_start = 1 is in there, that doesnt mean anything as far as i know

anyway, here's the problem in your version, the global variables ($_POST, $_GET, $_SESSION etc.) were introduced after version 4.1.0, so either you need to upgrade your php version or change the names of your variables from $_SESSION["key"] to $HTTP_SESSION_VARS["key"]
0
 
Zambian4evaAuthor Commented:
Okay, So I used a different machine with PHP 4.1.1, and it passes the session variables perfectly. 4.0.4 will not do this.

Thanks for all your help.
0
 
lozlozCommented:
do i not get any points? ;[
0
 
lozlozCommented:
much appreciated
0
 
Zambian4evaAuthor Commented:
Is there any way to give more than 500? Cause really you basically walked me through everything, and I do appreciate all that help.
0
 
lozlozCommented:
i think the grade determines the final points given but im not too sure
0
 
vishwanath1Commented:
I am new to PHP and am trying to write a script to process a form where a user submits a user id, the script then checks a text file on the server to see if the user id exist. If the user id exist, the script goes onto the next step, otherwise it gives them an error telling them the user id does not exist. If someone could help me it would be great.

-Vish
mailto_vishwanath@yahoo.com
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 9
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now