Jump to content

Recommended Posts

I am trying to pass a session variable from one page to another, but every time I use the following code you are directed to the right page, but it will not display the content of that page. It seems as if the variable is never making it to that next page. I also put in a simple echo command to let me know if the statement on the next page is true, but I got what I asked for the words "Nope". Any suggestions.

 

----------------------

index.php:

 

<?php

  session_start();

  include "db_connect.php";

  if(!isset($_POST['submit']))

  {

?>

 

//Display the page here

 

<?php }else{

 

  $user = protect($_POST['user_name']);

  $pass = protect($_POST['password']);

 

if($user && $pass)

{

$pass =($pass); //compare the encrypted password

$sql="SELECT user_name,company_name FROM `user` WHERE `user_name`='$user' AND `password`='$pass'";

$query=mysql_query($sql) or die(mysql_error());

 

    if(mysql_num_rows($query) > 0)

    {

      $row = mysql_fetch_assoc($query);

      $_SESSION['user_name'] = $row['user_name'];

      $_SESSION['company_name'] = $row['company_name'];

   

    echo "<script type=\"text/javascript\">window.location=\"client-resources.php\"</script>";

    }

    else

  {

    echo "<script type=\"text/javascript\">

  alert(\"Username and password combination is incorrect!\");

  window.location=\"index.php\"</script>";

  } 

}

else

{       

  echo "<script type=\"text/javascript\">

  alert(\"You need to gimme a username AND password!\");

  window.location=\"index.php\"</script>";

}}?>

 

-----------------------------------------------------------------------------

redirected page code:

 

<?php

  session_start();

?>

 

<div id="main-content"> 

<?php

if(isset($_SESSION['user_name']))

{

$user_name = $_SESSION['user_name'];

 

include ("inc/client-resources/client-resources.php");

}else{

echo "Nope";

?>

Link to comment
https://forums.phpfreaks.com/topic/180858-solved-help-with-windowlocation/
Share on other sites

I tried your suggestion and commented out the window.location line and replaced it with a

echo $row['user_name'];

This is what it looks like now:

 }else{

  $user = protect($_POST['user_name']);
  $pass = protect($_POST['password']);

if($user && $pass)
{
$pass =($pass); //compare the encrypted password
$sql="SELECT user_name,company_name FROM `user` WHERE `user_name`='$user' AND `password`='$pass'";
$query=mysql_query($sql) or die(mysql_error());

    if(mysql_num_rows($query) > 0)
    {
      $row = mysql_fetch_assoc($query);
      $_SESSION['user_name'] = $row['user_name'];
      $_SESSION['company_name'] = $row['company_name'];
     echo $row['user_name'];
     //echo "<script type=\"text/javascript\">window.location=\"client-resources.php\"</script>";
    }
    else
   {
    echo "<script type=\"text/javascript\">
   alert(\"Username and password combination is incorrect!\");
   window.location=\"index.php\"</script>";
   }   
}
else
{         
   echo "<script type=\"text/javascript\">
   alert(\"You need to gimme a username AND password!\");
   window.location=\"index.php\"</script>";
}}

 

I got the correct name from the database "admin". It seems like the database connection is there, but I cannot get it to pass that variable to the next page.

 

for starters, lose the javascript redirection (window.location), and use the header() function:

 

header ('Location: client-resources.php'); exit (0);

 

where client-resources.php is whatever file/page you wish to redirect to .. you must enter a path to that file, ie:

 

header ('Location: /path/to/file/client-resources.php'); exit (0);

 

if it requires one.

 

ok,

 

try uploading a simple test page to test sessions on your uploaded server:

 

<?php
session_start()

$_SESSION['stanley_cup'] = 'Leafs Win! Leafs Win!';

echo $_SESSION['stanley_cup'];
?>

 

let's just see that you can write sessions to the server properly.

I put in your recomened code and it looks like this:

 }else{

  $user = protect($_POST['user_name']);
  $pass = protect($_POST['password']);

if($user && $pass)
{
$pass =($pass); //compare the encrypted password
$sql="SELECT user_name,company_name FROM `user` WHERE `user_name`='$user' AND `password`='$pass'";
$query=mysql_query($sql) or die(mysql_error());

    if(mysql_num_rows($query) > 0)
    {
      $row = mysql_fetch_assoc($query);
      $_SESSION['user_name'] = $row['user_name'];
      $_SESSION['company_name'] = $row['company_name'];
     
     header ('Location: client-resources.php'); exit (0);
    }
    else
   {
    echo "<script type=\"text/javascript\">
   alert(\"Username and password combination is incorrect!\");
   window.location=\"index.php\"</script>";
   }   
}
else
{         
   echo "<script type=\"text/javascript\">
   alert(\"You need to gimme a username AND password!\");
   window.location=\"index.php\"</script>";
}}

 

When I tested the page I get the white screen of death :) The header() function isn't being executed. The page just refreshes. I also put in your test page code, but with the previous code never executing it never gets to that page.

sorry, what i should've said is add the following to a file, name it test.php (or whatever you like), and run it separately just to test your servers abilities to execute sessions:

 

<?php
session_start();

$_SESSION['stanley_cup'] = 'Leafs Win! Leafs Win!';

echo $_SESSION['stanley_cup'];
?>

 

just a simple test .. i haven't much time today to really invest today, so these little things will help find out the problem.

Your code for a test page was successful. The page did display Leafs Win! Leafs Win!

 

I thought error reporting was turned on, but after reviewing the code it was not.

The error at the top of the page before submitting the user name is:

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/vhosts/######/httpdocs/#######/index.php:1) in /var/www/vhosts/######/httpdocs/######/index.php on line 7

After looking up that error in a Google search it seems that because I had my error reporting code above my session_start(); it would not execute the session start. I guess I have to have my session_start(); as the very first line in my code. Does this make sense to you guys?

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.