Solved

Passing Variables to another page after Redirecting a Page

Posted on 2003-10-21
23
4,815 Views
Last Modified: 2007-12-19
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
Comment
Question by:Zambian4eva
  • 10
  • 9
  • 3
  • +1
23 Comments
 
LVL 11

Expert Comment

by:shmert
ID: 9592580
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
 
LVL 11

Expert Comment

by:shmert
ID: 9592590
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
 

Author Comment

by:Zambian4eva
ID: 9592683
Is there any way to pass them without displaying them? Because I don't those values to be seen by the end user.
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9592841
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
 
LVL 11

Expert Comment

by:shmert
ID: 9595308
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
 

Author Comment

by:Zambian4eva
ID: 9600023
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
 
LVL 13

Expert Comment

by:lozloz
ID: 9600084
you need session_start(); at the top of your page on index.php
0
 

Author Comment

by:Zambian4eva
ID: 9600311
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
 
LVL 13

Expert Comment

by:lozloz
ID: 9600360
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
 

Author Comment

by:Zambian4eva
ID: 9600787
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
 
LVL 13

Expert Comment

by:lozloz
ID: 9600885
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Zambian4eva
ID: 9600968
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
 

Author Comment

by:Zambian4eva
ID: 9601058
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
 
LVL 13

Expert Comment

by:lozloz
ID: 9601059
what version of php are you using incidentally
0
 

Author Comment

by:Zambian4eva
ID: 9601139
4.0.4
0
 

Author Comment

by:Zambian4eva
ID: 9601161
It is running on apache...
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9601181
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
 

Author Comment

by:Zambian4eva
ID: 9601515
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
 
LVL 13

Accepted Solution

by:
lozloz earned 500 total points
ID: 9602128
do i not get any points? ;[
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9606452
much appreciated
0
 

Author Comment

by:Zambian4eva
ID: 9606592
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
 
LVL 13

Expert Comment

by:lozloz
ID: 9606680
i think the grade determines the final points given but im not too sure
0
 

Expert Comment

by:vishwanath1
ID: 10319936
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
get radio button vale in array 7 37
Time difference 10 35
update field on focusout 15 24
Creating Image Thumbnails Using PHP 3 16
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now