SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    redirect not working, check my code.

    Hi,

    I click on account page and it lets me go to account page immediately without logging require, though i set the requirement. Here is the code,conditioning that user must login to view this section and some database info taken out to fill user's info.

    Code PHP:
    <?php 
     
    session_start();
     
    require('connect.php');
     
    if(isset($_SESSION['id']))
    {
    	$_SESSION['id']= $id;
     
        $result = mysql_query("SELECT id, username, email, name, land_line, mobile, address, city, region,feedback FROM user WHERE id= ('{$id}') ") or die(mysql_error());
     
        if(mysql_num_rows($result) == 1)
    	{
    		$row = mysql_fetch_array($result);
     
    		require('my_account.php');
        }
    }else
    {             
        echo "<script type='text/javascript'> document.location.href='http://www.mytable.vn/log_in.php' </script>";
    } 
     
     
    ?>

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    location is part of window, not document.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Location
    sudo rm -rf /
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace
    document.location.href
    with
    location.href

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replaced but not help

  5. #5
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    Hi,

    I click on account page and it lets me go to account page immediately without logging require, though i set the requirement. Here is the code,conditioning that user must login to view this section and some database info taken out to fill user's info.

    Code PHP:
    <?php 
     
    session_start();
     
    require('connect.php');
     
    if(isset($_SESSION['id']))
    {
    	$_SESSION['id']= $id;
     
        $result = mysql_query("SELECT id, username, email, name, land_line, mobile, address, city, region,feedback FROM user WHERE id= ('{$id}') ") or die(mysql_error());
     
        if(mysql_num_rows($result) == 1)
    	{
    		$row = mysql_fetch_array($result);
     
    		require('my_account.php');
        }
    }else
    {             
        echo "<script type='text/javascript'> document.location.href='http://www.mytable.vn/log_in.php' </script>";
    } 
     
     
    ?>
    First of all, your redirect is probably better if you send a header directly from PHP instead of letting JS do it. http://us3.php.net/manual/en/function.header.php

    Secondly, this piece of code is most likely incorrect:

    PHP Code:
        $_SESSION['id']= $id
    You probably mean:

    PHP Code:
        $id $_SESSION['id']; 

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed the code and I have this error

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/onestar/public_html/php/account_script.php:1) in /home/onestar/public_html/php/account_script.php on line 3

    Warning: Cannot modify header information - headers already sent by (output started at /home/onestar/public_html/php/account_script.php:1) in /home/onestar/public_html/php/account_script.php on line 19

    Code PHP:
    <?php 
     
    session_start();
     
    include('connect.php');
     
    if(isset($_SESSION['id'])){
     
    	$id = $_SESSION['id'];
     
        $result = mysql_query("SELECT id, username, email, name, land_line, mobile, address, city, region,star FROM user WHERE id= ('{$id}') ") or die(mysql_error());
     
        if(mysql_num_rows($result) == 1){
     
    		$rows = mysql_fetch_array($result);
     
        }
     
    }else{ header('location: [url]http://www.mytable.vn/login.php');[/url]
     
    } 
     
    ?>

  7. #7
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    I fixed the code and I have this error

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/onestar/public_html/php/account_script.php:1) in /home/onestar/public_html/php/account_script.php on line 3
    session_start() must be the first thing you call. You have to call it before your server sends its HTTP headers to the client browser.

  8. #8
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    session_start() must be the first thing you call. You have to call it before your server sends its HTTP headers to the client browser.
    I don't know if i called something else first accidently but I did put session_start() in the head of everybody else

    Here's my file structure

    account.php:

    <?php
    include('account_script.php')
    ...
    html...
    ...
    ?>



    account_script.php:

    <?php
    session_start()
    ...
    some php code...
    ...
    ?>

    so what could possibly be wrong ?

  9. #9
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried to test in locahost, there is not a problem

    I wonder if problem is laying under my internet hosting?

  10. #10
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    I don't know if i called something else first accidently but I did put session_start() in the head of everybody else

    Here's my file structure

    account.php:

    <?php
    include('account_script.php')
    ...
    html...
    ...
    ?>



    account_script.php:

    <?php
    session_start()
    ...
    some php code...
    ...
    ?>

    so what could possibly be wrong ?
    You probably have an extra space or newline character in one of your files. If you send anything to the browser before you call session_start() it will fail. Even a simple space or newline.

    As a precautionary measure for part of this, it is considered a bad coding practice to close your PHP tags when the file only contains PHP.

    Quote Originally Posted by salahsoftware View Post
    if you are using header redirect, then don't start session because that will send the header without redirect and you will receive the warning. Redirect should be the first response of a request and in fact the only response.
    This is incorrect. You are not understanding his problem. He is not receiving a warning from the result of calling header() after session_start(), he is getting the warning because he is sending other header information before calling session_start().

  11. #11
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes both the functions wont work and give warning if any output is before those functions in the script. If you are compelled to use those functions then you can output buffering (see ob_start()).

    @samanime:
    document has location (but location does have href along with document as far as i know. So document.location = 'location/to/go' should work for you.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  12. #12
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,035
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Take out the black line between <?php and session_start() and check for any spaces on the same line as <?php as even 1 space can be enough for the script to stop sending headers. Check for the same in account.php
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  13. #13
    SitePoint Enthusiast archanapatel's Avatar
    Join Date
    Aug 2008
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are setting cookie or session in coding then you can not redirect using header() of php.

    Better to use javascript redirect only.

  14. #14
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by archanapatel View Post
    If you are setting cookie or session in coding then you can not redirect using header() of php.
    Can you give an example?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  15. #15
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    Take out the black line between <?php and session_start() and check for any spaces on the same line as <?php as even 1 space can be enough for the script to stop sending headers. Check for the same in account.php
    I've checked less than 101 times for spaces, but still not work.

    Actually, I am gonna post the problematic code right now

    account.php (html)

    Code PHP:
    <?php include('php/account_script.php');?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Kami-Account</title>
    <link href="css/main_css.css" rel="stylesheet" type="text/css" />
    <link href="css/account.css" rel="stylesheet" type="text/css"/>
    <?php include('ssi/header.php');?>
    	<table width="550" cellpadding="0" cellspacing="0" class="margin_left35px margin_bottom20px border_bottom" id="table1">
    	<tr>
    		<th colspan="4" class="info left_align">Sản phẩm đang mua và bán</th>
    	</tr>
    	<tr>
    		<td width="129" class="right_align border_left">
    			<ul><li>Mua:</li></ul>
    		</td>
    		<td width="144"><ul>
    			<li> <a href="watching.php">Theo giỏi</a></li>
    			<li> <a href="won.php">Thắng</a></li>
    			<li> <a href="lost.php">Thua</a></li>
    		</ul></td>
    		<td width="70" class="right_align  border_left">
    			<ul><li>Bán:</li></ul>
    		</td>
    		<td width="205" class="border_right"><ul>
    			<li> <a href="selling.php">Đang bán</a> </li>
    			<li> <a href="sold.php">Đă bán</a> </li>
    			<li> <a href="unsold.php">Hết hạn</a> </li>
    		</ul></td>
    		</tr>
    	</table>
    	<table width="550" cellpadding="0" cellspacing="0" class="margin_left35px  border_bottom" id="table2">
    	<tr>
    		<th colspan="2" class="info left_align">Thông tin cá nhân</th>
    	</tr>
    	<tr>
    		<td width="133" class="right_align border_left">&nbsp;</td>
    		<td width="415" class="border_right margin_left30px"><span><?php echo '<a href="#" class= bold>' . $rows['username'] . '</a>'.' '. '( <a href="#">' .$rows['feedback'] . '</a> )'; ?></span></td>
    	</tr>
    	<tr>
    		<td class="right_align border_left">Email:</td>
    		<td class="border_right"><span><?php echo $rows['email']; ?></span></td>
    	</tr>
    	<tr>
    		<td class="right_align border_left">Tên:</td>
    		<td class="border_right"><span><?php echo $rows['name']; ?></span></td>
    	</tr>
    	<tr>
    		<td class="right_align border_left">Địa chỉ:</td>
    		<td class="border_right"><span><?php echo $rows['address']; ?></span></td>
    	</tr>
    	<tr>
    		<td class="right_align border_left">Thành phố:</td>
    		<td class="border_right"><span><?php echo $rows['city']; ?></span></td>
    	</tr>
    	<tr>
    		<td class="right_align border_left">Khu vực:</td>
    		<td class="border_right"><span><?php echo $rows['region']; ?></span></td>
    	</tr>
    	<tr>
    		<td class="right_align border_left">Điện thoại bàn:</td>
    		<td class="border_right"><span><?php echo $rows['land_line']; ?></span></td>
    	</tr>
    	<tr>
    		<td class="right_align border_left">Di động:</td>
    		<td class="border_right"><span><?php  echo $rows['mobile']; ?></span></td>
    	</tr>
    	<tr>
    		<td class="border_left"></td>
    		<td id="edit" class="border_right"><a class="editpass" href="#">Đổi password</a><a class="editinfo" href="#">Chỉnh sửa thông tin cá nhân</a></td>
    	</tr>
    	</table>
    <?php include('ssi/footer.php');?>


    account_script.php (php code)

    Code PHP:
    <?php
    session_start();
     
    include('config.php');
     
    require('connect.php');
     
    if(isset($_SESSION['username']))
    {
    	$username = $_SESSION['username'];
     
    	$result = mysql_query("SELECT id, username, email, name, land_line, mobile, address, city, region, feedback FROM user WHERE username= ('{$username}') ") or die(mysql_error());
     
    	if(mysql_num_rows($result) == 1)
    	{
    		$rows = mysql_fetch_array($result);		
    	}
    }else{header('location:'.'URL'.'login.php');}
     
    ?>

    The point of these two files is, when user click on the link 'account', if they've already logged in, they can see the content. Else they will redirected to login.php for logging in.

  16. #16
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,035
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    I've checked less than 101 times for spaces, but still not work.

    Actually, I am gonna post the problematic code right now

    account.php (html)

    [highlight="PHP"]<?php include('php/account_script.php');?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd".
    There's the culprit. That space between the <?php and include
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  17. #17
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    There's the culprit. That space between the <?php and include
    I think thats space is compulsory, isnt it ?

  18. #18
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I test the website locally, which is my on my pc, there is no header error, but when testing on remote, the scenario was as well mentioned as the topic.

    Any possibility that the remote server isn't working properly ? something not right with the config.ini on the remote ?

    Or localhost and remote host are completely different ? Could you clarify this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •