New to PHP... HELP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ckeezer
Forum Newbie
Posts: 1
Joined: Fri Oct 26, 2007 12:52 pm

New to PHP... HELP

Post by ckeezer »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am not a PHP developer, but I have been given a site that we need to host, which is in PHP. I have copied all of the files to my server and uploaded the db to my MySQL server. Everything on the site runs perfect, except for my admin page. I cannot my page to redirect to the proper page. Here is the code... again I did not write this so I have limited knowledge on why things are the way they are:

Code: Select all

// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "home.php";
  $MM_redirectLoginFailed = "index.php?login=failed";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_connection, $connection);
  
  $LoginRS__query=sprintf("SELECT username, password FROM admin_users WHERE username='%s' AND password='%s'",
    get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)) or die(mysql_error()); 
   
  $LoginRS = mysql_query($LoginRS__query, $connection) or die(mysql_error());
  
  $loginFoundUser = mysql_num_rows($LoginRS) or die(mysql_error());
  
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
  
}
The part that is not working would be the very last IF statement. On the site that is currently active this page redirects to: http://www.mysite.com/admin/home.php

on my server the page redirects to: http://www.mysite.com/admin/index.php?a ... 2Fhome.php

Can anyone help me out on resolving this? Thanks,

CK


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]2.[/b] Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.[/quote]
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

I cannot my page to redirect to the proper page. Here is the code... again I did not write this so I have limited knowledge on why things are the way they are:
So what makes you think any of us are better equipped??? :P

Anyways...If your having troubles redirecting to the wrong URL I suspect...that it's probably the variables being appended to header()...why not run some tests on those and find out why they are being set incorrectly?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Where does the page redirect to?
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

Trust me, I am no PHP expert but this is my thought:

Code: Select all

if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; 
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
I think that you are right, please figure out what the PrevURL is, because it is saying that $MM_redirectLoginSuccess equals your stored session's previous url. So when the 'if' statement is true it goes to the previous URL that was stored in the session.

This is what I would do, make a backup of the current php file, then change the header("location: ".$MM_redirectLoginSuccess); to

Code: Select all

header("location: http://www.mysite.com/admin/home.php ")
Keep us updated:

--
Greg
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Full URLs should be passed to the header() funtion, as per the HTTP spec.
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

I know it's not my post, but Everah - what does that mean?

--
Greg
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
header('Location: http://www.someurl.com/path/to/the/uri/');
?>
In the notes section of the manual on header() you'll find:
PHP Manual for header() wrote: Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

Code: Select all

<?php
    /* Redirect to a different page in the current directory that was requested */
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 'mypage.php';
    header("Location: http://$host$uri/$extra");
    exit;
    ?>
Note: Session ID is not passed with Location header even if session.use_trans_sid is enabled. It must by passed manually using SID constant.
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post by gregwhitworth »

Very interesting......

--
Greg
Post Reply